【发布时间】:2017-04-21 06:11:51
【问题描述】:
我有一个包含 2 个部分视图的视图。
@model ListViewModel
....
@{
Html.RenderPartial("EditCartItemsPartical");
Html.RenderPartial("ShowProductINFO", Model.Products);
}
我只想用第一个部分创建一个 from,用第二个部分创建一个列表。
部分视图
EditCartItemsPartical.cshtml
@model TestNewATM3._0.Models.CartItem
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.CartId, "CartId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CartId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CartId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductId, "ProductId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" })
</div>
</div>
.... // more control for CartItem
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
ShowProductINFO.cshtml
@model IEnumerable<TestNewATM3._0.Models.AllProduct2>
<p>@Html.ActionLink("Create New", "Create")</p>
<table class="table">
<tr>
<th>ID</th>
<th>@Html.DisplayNameFor(model => model.Title)</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(model => item.Id)</td>
<td>@Html.DisplayFor(modelItem => item.Title)</td>
</tr>
}
</table>
在我的控制器中我得到了这个。
public ActionResult Edit()
{
var db = ApplicationDbContext.Create();
var list = new ListViewModel
{
Products = db.AllProduct2s.ToList(),
Users = db.Users.ToList(),
Cart = db.Carts.ToList(),
CartItem = db.CartItems.ToList()
};
return View(list);
}
但问题是我不能同时显示两个部分,因为如果我在我看来发送list,那么我的第一个部分会出现问题,因为它需要@model TestNewATM3.0.Models.CartItem。但如果我不发送列表我的列表将不会显示,因为我不发送它。
那么如何同时显示一个普通的局部表单视图和一个列表局部视图呢?
【问题讨论】:
-
只需
Html.RenderPartial("EditCartItemsPartical", new CartItem());(将CartItem的新实例传递给部分视图)。 -
您可能需要阅读 this question/answer 以了解您可能遇到的错误(即使您没有在问题中包含它)
-
谢谢我会看看它
标签: asp.net-mvc visual-studio razor