Reputation: 91
I am trying to figure out how to pass the model object from controller to view in a following scenario:
<% Html.Action("GetRequest", "The_Controller", new { requestId = 12 }); %>
<% Html.RenderPartial("Request", ViewData.Model); %>
The action should, and it does, call the controller's method which looks up the Request object in the DB repository and returns a Request object.
The partial view, named Request, should receive the request object and display it.
The problem is that the ViewData.Model is always null?!
I have tried to figure it out, but with no success :(
The reason for this behavior is that I need to display a partial view inside the jQuery's modal dialog based on the requestId value provided by the jQGrid. I have reached the point where I open the dialog but can't get that model object instance.
Any help is appreciated.
The solution - With the help of Nick Larsen and Darin Dimitrov
The controller:
[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public PartialViewResult GetRequest(string requestId)
{
Request request = DatabaseContext.GetRequest(Convert.ToInt32(requestId)) as Request;
return PartialView("Request", request);
}
The view's Javascript:
function OpenRequest(requestId) {
var methodName = '<%= Url.Content("~")%>' + 'Controller/GetRequest/';
var dataType = "html";
var postData = { requestId: requestId };
var contentType = "application/x-www-form-urlencoded"; ;
var request = ContactServer(methodName, postData, dataType, contentType);
$("#dialog").html(request);
$("#dialog").dialog({ html: request, title: 'Request details...', width: 800, height: 600, modal: true, zindex: 300000, show: 'scale', resizable: false });
}
Upvotes: 1
Views: 3324
Reputation: 1039398
You could have the controller action return the partial view:
<%= Html.Action("GetRequest", "The_Controller", new { requestId = 12 }) %>
And in your controller action:
public ActionResult GetRequest(int requestId)
{
var request = _repository.GetRequest(requestId);
return PartialView("Request", request);
}
This way the GetRequest
action will pass the request object to the strongly typed Request.ascx
partial view and include it in the page at the place you called Html.Action
helper.
Upvotes: 2
Reputation: 100627
It seems that you've got all the necessary information, up front, to create a 'Request' object in your initial controller action method.
Suggest that:
drop the <% Html.Action("GetRequest", "The_Controller", new { requestId = 12 }); %>
altogether. We'll be prepopulating ViewData with all the data required to send to the Partial.
in your initial controller method, you create a new ViewData entry, perhaps ViewData["SomeRequest"]
prepopulate that as you need on the initial controller method, using requestId = 12
and email = "foo@bar"
, and all the other relevant pieces to create a 'Request' object, or whatever is needed in the Partial View named 'Request'.
i.e. ViewData["SomeRequest"]= dbRepository.GetRequestById(intrequestId);
in your View, call Html.RenderPartial("Request", ViewData["SomeRequest"]);
Upvotes: 1
Reputation: 18877
It's better to set it up where your view does not call your controller. Load all of the data for the request in the action which calls this view and populate a view model with the needed data. Once you have that, render the fields from the model.
As for your actual problem. The action that calls this view in the first place populates the ViewData.Model for its context. When you call the action method, the framework is creating a new context with its own ViewData which you dont have access to without a handle to that newly created context.
Upvotes: 1