c# - ASP.NET MVC 我正在尝试使用(部分 View )在同一页面中添加评论产品(http post)

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

_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/

相关文章:

c# - dapper 和decimal(10,2) 的无效转换

c# - 在 ASP.NET WebForms 项目中的什么地方可以找到 "System.Web.UI.Control"命名空间?

c# - 如何只允许每个用户一个事件 session

c# - 如何在 ASP.NET MVC 中更改菜单标题(垂直到水平)

c# - WPF 文本框绑定(bind)到数字或日期时间字段/属性

c# mssql sql连接和多个插入

c# - 动态创建服务引用和使用服务

javascript - 从带有主文件的 aspx 文件调用时,jQuery 代码不起作用

c# - 在 ASP.NET MVC 中使用按位标志

html - 将 bootstrap CSS 类属性添加到文本框 ASP.Net MVC