C# ASP.NET MVC I';我正在尝试使用(部分视图)在同一页面中添加评论产品(http post)
C# ASP.NET MVC I';我正在尝试使用(部分视图)在同一页面中添加评论产品(http post),c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,\u getProductReviews.cshtml: 我这样称呼我的局部观点: <p>@Html.Partial("_CreateR");</p> ProductController: // GET: /Product/Create public ActionResult Create() { return View(); } // // POST: /Product/Create [HttpPost] [ValidateAntiForgeryToken
\u getProductReviews.cshtml
:
我这样称呼我的局部观点:
<p>@Html.Partial("_CreateR");</p>
ProductController
:
// 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的简单视图时,它可以工作,但当我尝试使用部分视图时,会显示此消息
传递到字典的模型项的类型为“System.Collections.Generic.List`1[Commerce.Domain.Entites.t_review]”,但此字典需要类型为“Commerce.Domain.Entites.t_review”的模型项
Html.Partial
在不首先调用控制器的情况下呈现视图
Html.Action
调用一个控制器
Action
,它可以像任何东西一样返回:视图、部分视图、json等等
一般来说,只有当您希望显示静态内容或有权访问所需的模型时,才应使用Html.Partial
。对于其他所有内容,如您想从服务器返回更多数据,请使用Html.Action
消息应该是显而易见的-\u getProductReviews.cshtml
的模型是List
,然后将其传递给\u CreateR.cshtml
,其模型是t\u review
(而不是List
)我可以使用“Html.Action”在同一页面中调用我的视图吗?您不能在另一个视图中加载视图,但可以使用Html.Action在视图中呈现部分视图。那么,我如何在底部使用按钮列出我的产品评论,以添加一个,全部在同一视图中?使用一个(或多个)在你的视图中显示部分视图。我确实这样做了,但我不知道如何添加一个与facebook中的评论保持相同视图的列表评论。
// 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);
}