pass a different model to the partial view

若如初见. 提交于 2019-11-30 11:17:03

you are using the right method but not passing in the right arguments

you might want to try it like this:

@Html.Partial("~/[path_to_root_only_if_exists]/_CreateUniFunctionPartial.cshtml", new Application.ViewModels.PartialViewModel())

if you do not pass in a model, it will automatically take the one from its parent, which in your case is

Application.ViewModels.Model1ViewModel

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"))
 {
      <div>
          @Html.AntiForgeryToken()
          @Html.TextBoxFor(m => m.SomeProperty)
          <input type="submit" value="Save" />
      </div>
 }

This is quite simple to do. There is a html directive which can render a partial view. Following is the code sample:

 @Html.Partial("nameOfPartial", Model)

Now here Model could be from your main controller.

or you can define a new controller action with partialviewresult as return type and try to render it in the page like this:

@{Html.RenderAction("Someaction", "somecontroller");}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!