How to pass Model from a view to a partial view?

后端 未结 2 830
滥情空心
滥情空心 2021-01-02 09:20

I have a view that is not strongly typed. However I have in this view a partial view that is strongly typed.

How do I do I pass the model to this st

相关标签:
2条回答
  • 2021-01-02 09:43

    Put the object required by the partial into Viewdata and use ist in the view as input for the partial.

    public ActionResult Test()
            {
                  ViewData["DataForPartial"] = new PartialDataObject();
                  return View("Test")
            }
    

    In the view use:

    <% Html.RenderPartial("PartialView",ViewData["DataForPartial"]); %>
    

    But anyway: There is no reason no to have a stronly typed view.

    0 讨论(0)
  • 2021-01-02 09:47

    You should extend your model so that it can provide all necessary fields for the view (this is called ViewModel) or you provide them seperately with ViewData.

     public ActionResult Test()
            {
                  MyData = new Data();
                  MyData.One = 1;
                  ViewData["someData"]=MyData;
                  return View();
            }
    

    then:

    <% Html.RenderPartial("PartialView",ViewData["someData"]); %>
    

    ViewData is a nice losely typed dictionary

    0 讨论(0)
提交回复
热议问题