views:

138

answers:

1

I'm loading a partial view with an AJAX call:

public ActionResult LoadServerForm()
        {
            //data stuff

            ViewData["ApplicationID"] = appID.ToString();
            ViewData["Servers"] = ServersList(appServerRep.Session, null, appServers);
            return PartialView("Application_AddServer");
        }

This works great, but I'm trying to get away from magic ViewData strings. I tried making the partial view inherit from the same ViewModel as the "hosting" page, but the Model object is null when I try to this in the partial view:

<%= Html.HiddenFor(model=>model.Application_Key, Model.Application_Key) %>

Is there a way to pass the main page ViewModel down into the AJAX-loaded PartialView or should I be looking for a different approach altogether?

+1  A: 

When you return PartialView("Application_AddServer");, you have to pass the model:

return PartialView("Application_AddServer", model);

Since this is an AJAX request, it's a separate controller action invocation, and the new PartialView doesn't know about the model of the requesting page. You'll have to reconstruct it, either from whatever your original data source is or from data passed with the AJAX request.

kevingessner
OK now I feel like a DUMMY... I could swear I checked to see if there was an overload to include the model, but apparently not!!
Dave Swersky
Hey, happens to everyone at times :)
RedFilter