_getProductReviews.cshtml
:
我这样调用我的部分 View :
<p>@Html.Partial("_CreateR");</p>
_CreateR.cshtml
:
此代码由 Controller 自动生成:
@model Commerce.Domain.Entites.t_review
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>t_review</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.text, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.text)
@Html.ValidationMessageFor(model => model.text)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.title, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.customer_id, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.customer_id)
@Html.ValidationMessageFor(model => model.customer_id)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.product_fk, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.product_fk)
@Html.ValidationMessageFor(model => model.product_fk)
</div>
</div>
<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>
}
产品 Controller
:
// GET: /Product/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Product/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,text,title,customer_id,product_fk")] t_review review)
{
if (ModelState.IsValid)
{
prose.CreateReview(review);
return RedirectToAction("Index");
}
return View(review);
}
当我使用带有 actionlink 的简单 View 时它可以工作,但是当我尝试使用部分 View 时显示此消息
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Commerce.Domain.Entites.t_review]', but this dictionary requires a model item of type 'Commerce.Domain.Entites.t_review'.
最佳答案
Html.Partial
呈现一个 View 而不首先调用Controller
。
Html.Action
调用一个 Controller
Action
,它可以像任何东西一样返回: View 、部分 View 、json 等。
作为一般准则,您应该仅在要显示静态内容或有权访问所需模型时才使用 Html.Partial
。对于其他一切,比如当您想从服务器返回更多数据时,请使用 Html.Action
。
关于c# - ASP.NET MVC 我正在尝试使用(部分 View )在同一页面中添加评论产品(http post),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27213273/