pass a different model to the partial view

后端 未结 3 1577
忘掉有多难
忘掉有多难 2021-01-01 15:24

I am trying to pass a different model to the partial view from a view. I have two separate controller actions for both of them and two different view models. But when I call

3条回答
  •  耶瑟儿~
    2021-01-01 15:42

    One thing you will need to do is regenerate a model or utilize a property in the model. For example:

     public class OuterViewModel
     {
         public InnerViewModel InnerViewModel { get; set; }
     }
    
     public class InnerViewModel
     {
         public string SomeProperty { get; set; }
     }
    

    In the top page, you can accept the OuterViewModel, then pass the InnerViewModel to the Partial.

    Outer.cshtml:

     @model OuterViewModel
     @Html.Partial("_InnerPartial", Model.InnerViewModel)
    

    _InnerPartial.cshtml:

     @model InnerViewModel
     @using (Html.BeginForm("Inner", "Controller"))
     {
          
    @Html.AntiForgeryToken() @Html.TextBoxFor(m => m.SomeProperty)
    }

提交回复
热议问题