所以我有一个名为_CreateClient的局部视图需要传递一个ClientEditViewModal,它应该从控制器传递出来,如下所示:
public ActionResult Create()
{
ClientEditViewModel viewModel = new ClientEditViewModel();
return PartialView("_CreateClient", viewModel);
}
但是在我的索引视图中,我需要将partial调用为模态窗口,我这样做:
<div class="mfp-hide">
@Html.Partial("_CreateClient")
</div>
但是我的索引视图需要使用IEnumerable的ClientModel,当我尝试渲染索引视图时,我收到此错误:
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Project.Models.ClientModel]', but this dictionary requires a model item of type 'Project.ViewModels.ClientEditViewModel'.
如何将视图传递给_CreateClient并将其保存在模态中?或者有更好的方法来做这件事吗?
谢谢!
编辑:
指数行动:
public ActionResult Index()
{
IEnumerable<ClientModel> clients = clientService.GetAll();
return View(clients);
}
索引视图:
@model IEnumerable<TechUCRM.Models.ClientModel>
@{
Layout = "~/Views/Shared/_UserLayout.cshtml";
ViewBag.Title = "Index";
}
@section alert {
@if (!String.IsNullOrEmpty(ViewBag.alert))
{
<div class="alert alert-@ViewBag.alertType" role="alert">
@ViewBag.alert
</div>
}
}
<div class="row">
<div class="col-sm-6 text-left">
<h2>Clients</h2>
</div>
<div class="col-sm-6 text-right">
<label for="#add-client-btn">New Client</label>
<a href="@Url.Action("Create")" id="add-client-btn" class="btn btn-default btn-sm"><i class="fa fa-plus fa-fw"></i></a>
</div>
<div class="mfp-hide">
@Html.Partial("_CreateClient") <!--Need to be passed ClientCreateViewModel-->
</div>
</div>
<div class="row">
@foreach(var item in Model)
{
<div class="col-md-3 col-xs-12" style="margin-top:20px;">
<a href="" class="btn btn-primary btn-lg">@item.Name</a>
</div>
}
</div>
编辑:
好吧所以我想通了......我觉得有点荒谬,因为我需要做的就是给部分一个新的ViewModal ......是的
解答:
@Html.Partial("_CreateClient", new ClientCreateViewModal())
答案 0 :(得分:0)
@model ClientEditViewModel
@Html.Partial("_CreateClient", Model);
您可以将模型作为第二个参数传递。