我试图弄清楚如何填充我的局部视图的模型。例如,PartialView模型包括用户在注释对话框中输入的所有内容,例如“主题”和“注释内容”,但它还需要来自父页面的模型的唯一ID(即产品ID)。这样,当从对话框窗口保存便笺时,我可以将其保存为该特定产品。
在下面的示例中,如果用户在ParentPage上查看ID为12的产品并想要添加注释,则可以单击“注释”按钮以打开对话框窗口,填写主题和内容,然后单击“提交”。在提交给控制器的过程中,如果ProductId为12,则EntityModule应该=“ Product”,而EntityKey应该=12。我试图弄清楚NoteViewModel如何从父级ViewModel检索这两个字段。
ProductViewModel.cshtml
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
NoteViewModel.cshtml
public int Id { get; set; }
public string EntityModule { get; set; }
public int EntityKey { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
ParentPage.cshtml
@model MyApp.Web.ViewModels.Shared.ProductViewModel
@{ await Html.RenderPartialAsync("_NotesPartial"); }
_NotesPartial.cshtml
@model MyApp.Web.ViewModels.Shared.NoteViewModel
<button id="open" class="btn btn-primary">Notes</button>
@(Html.Kendo().Window()
.Name("window")
.Title("About Alvar Aalto")
.Visible(false)
.Draggable()
.Resizable()
.Width(600)
.Actions(actions => actions.Pin().Minimize().Maximize().Close())
.Content(@<text>
<form asp-action="_NotesPartial">
<div class="form-horizontal">
<h4>NoteViewModel</h4>
<hr />
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Subject" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Subject" class="form-control" />
<span asp-validation-for="Subject" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="Content" class="col-md-2 control-label"></label>
<div class="col-md-10">
<textarea asp-for="Content" class="form-control" cols="40" rows="4"></textarea><span asp-validation-for="Content" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form>
</text>)
)
<script>
$("#open").click(function () {
var win = $("#window").data("kendoWindow");
win.center();
win.open();
});
</script>
一种方法是通过父页面填充并传递模型。例如:
public class ProductViewModel {
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public NoteViewModel Note { get; set;}
}
然后,使用如下代码:
@{ await Html.RenderPartialAsync("_NotesPartial", Model.Note); }
MVC6中没有RenderAction,对于这样的目的,这是一个很好的解决方案:
Html.RenderAction("_NotesPartial", new { id = ProductViewModel.Id })
但您仍然可以在MVC5中使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句