ASP.NET MVC我尝试使用(部分视图)在同一页面中添加评论产品(http post)

时间:2014-11-30 12:33:23

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

_getProductReviews.cshtml

我这样称呼我的部分观点:

<p>@Html.Partial("_CreateR");</p>

_CreateR.cshtml

此代码由控制器自动生成:

@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>

}

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的简单视图时,它可以工作但是当我尝试使用部分视图时显示此消息

  

传递到字典中的模型项的类型为&#39; System.Collections.Generic.List`1 [Commerce.Domain.Entites.t_review]&#39;,但此字典需要类型为&的模型项#39; Commerce.Domain.Entites.t_review&#39;

1 个答案:

答案 0 :(得分:0)

Html.Partial呈现视图,不用首先调用Controller

Html.Action调用Controller Action,它可以像所有内容一样返回:视图,局部视图,json等。

作为一般准则,仅当您想要显示静态内容或有权访问所需模型时,才应使用Html.Partial。对于其他所有内容,例如,如果您想从服务器返回更多数据,请使用Html.Action