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


  传递到字典中的模型项的类型为“ System.Collections.Generic.List`1 [Commerce.Domain.Entites.t_review]”,但是此字典需要模型类型为“ Commerce.Domain.Entites.t_review”的模型项。

最佳答案

Html.Partial呈现视图而无需先调用Controller

Html.Action调用Controller Action,它可以像一切一样返回:视图,部分视图,json等。

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

09-11 19:04
查看更多