Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:38:56+00:00 2026-06-03T08:38:56+00:00

I have an MVC application that I’m using various JsonResult endpoints to populate the

  • 0

I have an MVC application that I’m using various JsonResult endpoints to populate the javascript ViewModel.

I have been using several jQuery Ajax requests to populate the model, but I’d like as much of the inital model to be passed to the view on the server.

The ViewModel has 3-5 pieces (depending on where the user is in the application):

  1. Basic page links, these don’t change very often and could be the exact same throughout the entire user’s session
  2. User notifications.
  3. User data.
  4. (optional) Viewable data
  5. (optional) misc data

I’m currently using this code to load the first three pieces:

$(document).ready(function () {

    ko.applyBindings(viewModel);
    @Html.Raw(ViewBag.Script)

    // Piece 1.  Almost always the same thing
    postJSON('@Url.Action("HomeViewModelJson", "Home")', function (data) {

        if (data == null)
            return;

        for (var i in data.Tabs) {
            viewModel.tabs.push({ name: data.Tabs[i] });
        }

        for (var i in data.Buttons) {
            viewModel.metroButtons.push({ name: data.MetroButtons[i] });
        }

        for (var i in data.Ribbons) {
            viewModel.ribbons.push(data.Ribbons[i]);
        }
        ApplyButtonThemes();
    });
});


// Piece 2.  Changes constantly. OK as is
postJSON('@Url.Action("GetNotifications", "NotificationAsync")', function (nots) {
    viewModel.notifications.removeAll();

    ko.utils.arrayForEach(nots, function (item) {
        item.readNotification = function () {
            hub.markNotificationAsRead(this.Id);
            return true;
        };
        viewModel.notifications.push(item);
    });
});

// Piece 3. Changes but should also be loaded at startup
postJSON('@Url.Action("GetUser", "UserAsync")', function (user) {
    viewModel.user(koifyObject(user));
});


postJSON = function(url, data, callback) {
    if($.isFunction(data)) {
        callback = data;
        data = {};
    }
    $.ajax({
        'type': 'POST',
        'url': url,
        'contentType': 'application/json',
        'data': ko.toJSON(data),
        'dataType': 'json',
        'success': callback
    });
};

I tried doing something like this, but I’m finding that by using the @Html.Action("HomeViewModelJson", "Home") is causing the HTTP headers to get changed and the whole page is sent as if it were JSON

       (function (data) {

            if (data == null)
                return;

            for (var i in data.Tabs) {
                viewModel.tabs.push({ name: data.Tabs[i] });
            }

            for (var i in data.MetroButtons) {
                viewModel.metroButtons.push({ name: data.MetroButtons[i] });
            }

            for (var i in data.Ribbons) {
                viewModel.ribbons.push(data.Ribbons[i]);
            }
            ApplyMetroButtonThemes();
        })('@Html.Action("HomeViewModelJson", "Home")');

What I’d like to do is use the existing JsonResult endpoints to get Json data into my ViewModel on the server side, before the page is sent to the user.

Are there any options that will allow me to do that w/o rewriting my controllers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:38:57+00:00Added an answer on June 3, 2026 at 8:38 am

    When rendering the main view you are using a view model, right? In this view model simply populate the properties that you don’t want to be fetched with AJAX before returning the view:

    public ActionResult Index()
    {
        MyViewModel model = ...
        model.Prop1 = ...
        model.Prop2 = ...
        return View(model);
    }
    

    for example if you have the following action that is used for the AJAX requests:

    public JsonResult GetProp1()
    {
        Property1ViewModel model = ...
        return Json(model, JsonRequestBehavior.AllowGet);
    }
    

    you could use it from the main action to populate individual properties:

    model.Prop1 = (Property1ViewModel)GetProp1().Data;
    model.Prop2 = (Property2ViewModel)GetProp2().Data;
    

    and then inside the corresponding view you could use the Json.Encode method to serialize the entire model into a JSON string:

    @model MyViewModel
    <script type="text/javascript">
        var model = @Html.Raw(Json.Encode(Model));
        // You could use model.Prop1 and model.Prop2 here
    </script>
    

    or you could also serialize individual properties if you don’t need all of them:

    @model MyViewModel
    <script type="text/javascript">
        var prop1 = @Html.Raw(Json.Encode(Model.Prop1));
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application that uses jQuery 1.3.2. On several of the
I have an mvc application that has been coded to use Windows authentication and
I have an MVC 2 application that has the following on it; <% using(
I have ASP.NET MVC application that has list with jQuery Selectable list. My problem
I have an ASP.NET MVC application that has a jQuery Treeview and a jQuery
I have a non-mvc application that needs to dynamically populate a select list, so
I have a spring mvc application that I am using Hibernate with. I am
I have a working zend mvc application that is using a layout, and this
We have an existing MVC application that we're building using TeamCity and things were
I have an MVC 2.0 application that requires a persistent login(if chosen) using forms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.