MVC pass model object from controller to view

前端 未结 3 1578
北海茫月
北海茫月 2021-01-25 00:41

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         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-25 01:20

    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"]);

提交回复
热议问题