我的模特:using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace SNW.Models.CustomerOrderInfo{ public class MemberOrder : CustomerOrder { public new int customerOrderID { get; set; } [..] public int customerOrderLineID { get; set; } public int productID { get; set; } [..] public double customerOrderQtyMin { get; set; } public double customerOrderQtyMax { get; set; } [..] }}我的控制器:[..]using SNW.Models;using SNW.Models.CustomerOrderInfo;namespace SNW.Controllers{ public class CustomerOrderController : Controller { private DBModelContainer db = new DBModelContainer(); [HttpPost, ActionName("SaveChanges")] public ActionResult SaveChanges([Bind(Include = "customerOrderQtyMax")] SNW.Models.CustomerOrderInfo.MemberOrder line) { if(ModelState.IsValid) { db.Entry(line).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("CustomerOrder"); } return View(line); } }}我的视图(CustomerOrder.cshtml):@model IEnumerable<SNW.Models.CustomerOrderInfo.MemberOrder>[..] @foreach (var MemberOrder in Model) { <tr> <td>@MemberOrder.productID</td> <td>@MemberOrder.productName</td> [..] <td>@Html.EditorFor(model => MemberOrder.customerOrderQtyMin)</td> <td> @using (Html.BeginForm("SaveChanges", "CustomerOrder", new { id = MemberOrder.customerOrderLineID })) { @Html.ValidationSummary(true) @Html.HiddenFor(model => MemberOrder.customerOrderLineID) @Html.EditorFor(model => MemberOrder.customerOrderQtyMax) @Html.ValidationMessageFor(model => MemberOrder.customerOrderQtyMax) <input type="submit" value="+" class="btn btn-default" /> } </td> [..] </tr>渲染视图:问题:每当我更改“订单行”的“最大值”并单击“ +”以在数据库中对其进行更新时,它将不起作用。line方法中的SaveChanges()值为0或为null,包括customerOrderQtyMax值为0.0,但是数据库更新也不会发生-先前的值将再次显示。所以我的问题是,如何仅从此IEnumerable视图将customerOrderQtyMax输入值传递给DB?我希望能够直接在CustomerOrder.cshtml页面中修改订单(或您希望的购物车)值。到目前为止,我尝试了什么:Updating Related Data with the Entity Framework in an ASP.NET MVC ApplicationVarious tutorials on Youtube将SaveChanges()方法与EF6为Edit()实体从数据库模型生成的CustomerOrderLines方法进行比较。后者不使用IEnumerables ,而是拥有自己的视图(Edit.cshtml)。由于订单行的主键由HiddenFor()和customerOrderLineID属性组成,因此在视图中添加了一个额外的customerOrderID字段。 @using (Html.BeginForm("SaveChanges", "CustomerOrder", new { id = MemberOrder.customerOrderLineID })) { @Html.ValidationSummary(true) @Html.HiddenFor(model => MemberOrder.customerOrderLineID) @Html.HiddenFor(model => MemberOrder.customerOrderID) @Html.EditorFor(model => MemberOrder.customerOrderQtyMax) @Html.ValidationMessageFor(model => MemberOrder.customerOrderQtyMax) <input type="submit" value="+" class="btn btn-default" /> }伙计们,谢谢您的帮助!编辑:我已经多次更改了SaveChanges()方法中的代码,但是这是我收到的错误:在SNW.Models.CustomerOrderInfo.MemberOrder中引用public ActionResult SaveChanges([Bind(Include = "customerOrderQtyMax")] SNW.Models.CustomerOrderInfo.MemberOrder line)会给我以下错误:{“实体类型MemberOrder不属于当前上下文模型的一部分。”}可能是因为它是我自定义的Model,它继承自CustomerOrder Model,因为可以由EF模型生成器覆盖CustomerOrder我需要使用额外的自定义属性进行计算将SNW.Models.CustomerOrderInfo.MemberOrder更改为原始CustomerOrderLine,并收到以下错误:{“存储更新,插入或删除语句影响了意外的行数(0)。实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。 “}。CustomerOrderLine由EF模型生成器生成,我希望其属性与CustomerOrder合并,以便能够在一个窗口中编辑定单的信息,而不是对每一行(定单行)进行“编辑视图”。这就是为什么我制作自定义SNW.Models.CustomerOrderInfo.MemberOrder模型的原因。现在SaveChanges()是public ActionResult SaveChanges([Bind(Include = "customerOrderQtyMax")] CustomerOrderLine line)@Jasen。 HTTP POST标头谢谢。Request URL:http://localhost:64778/CustomerOrder/SaveChanges/8Request Method:POSTStatus Code:500 Internal Server ErrorRequest Headersview parsedPOST /CustomerOrder/SaveChanges/8 HTTP/1.1Host: localhost:64778Connection: keep-aliveContent-Length: 68Cache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Origin: http://localhost:64778User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36Content-Type: application/x-www-form-urlencodedReferer: http://localhost:64778/CustomerOrder/CustomerOrderAccept-Encoding: gzip,deflate,sdchAccept-Language: en-GB,en;q=0.8,lt;q=0.6Cookie: .ASPXFORMSAUTH=F44D36B23D1EA17ABB24DEEC4AFA7B09FD2D7B3EEB8D9C57D95561E9A9B334F4BA90F46AEAC2E2DE23958998B3F888342E507B92484C45D990CD0FD08D38F8D8D994CC8F5C231A0144DE4A7B89A286A1AFFEE765C86C856E71403FF94FDF873EForm Dataview parsedMemberOrder.customerOrderLineID=8&MemberOrder.customerOrderQtyMax=78Response Headersview parsedHTTP/1.1 500 Internal Server ErrorCache-Control: privateContent-Type: text/html; charset=utf-8Server: Microsoft-IIS/8.0X-AspNet-Version: 4.0.30319X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcRG9uYXRhc1xTb3VyY2VcUmVwb3NcU291bmRzTmljZVdob2xlZm9vZHNcU05XXFNOV1xDdXN0b21lck9yZGVyXFNhdmVDaGFuZ2VzXDg=?=X-Powered-By: ASP.NETDate: Wed, 09 Apr 2014 21:03:23 GMTContent-Length: 17180HTML Form:<form action="/CustomerOrder/SaveChanges/8" method="post"> <input data-val="true" data-val-number="The field customerOrderLineID must be a number." data-val-required="The customerOrderLineID field is required." id="MemberOrder_customerOrderLineID" name="MemberOrder.customerOrderLineID" type="hidden" value="8"> <input class="text-box single-line" data-val="true" data-val-number="The field customerOrderQtyMax must be a number." data-val-required="The customerOrderQtyMax field is required." id="MemberOrder_customerOrderQtyMax" name="MemberOrder.customerOrderQtyMax" type="text" value="3"> <span class="field-validation-valid" data-valmsg-for="MemberOrder.customerOrderQtyMax" data-valmsg-replace="true"></span> <input type="submit" value="+" class="btn btn-default"></form>Tried a solution from this StackOverflow answer and changed the code to [HttpPost, ActionName("SaveChanges")] public ActionResult SaveChanges([Bind(Include = "customerOrderQtyMax")] CustomerOrderLine line) // whitelist fields { CustomerOrderLine lineTemp = new CustomerOrderLine() { customerOrderQtyMax = line.customerOrderQtyMax }; if(ModelState.IsValid) { db.Entry(lineTemp).State = EntityState.Added; db.SaveChanges(); return RedirectToAction("CustomerOrder"); } return View(lineTemp); }And now got this error: {"Cannot add or update a child row: a foreign key constraint fails (\"c1snw\".\"CustomerOrderLines\", CONSTRAINT \"FK_ProductCustomerOrderLine\" FOREIGN KEY (\"productID\") REFERENCES \"Products\" (\"productID\") ON DELETE NO ACTION ON UPDATE NO ACTION)"}, which probably means... well, I'm not sure if correct line is selected from DB using this code and whether it is trying to Update or Add a new line. Will check that.SOLUTION:@Jasen's suggestion of using Prefix="MemberOrder" in Bind helped to get code working!However, I got rid of Include= statement in Bind, because it only worked when all line's attributes, that are not initially empty (null or 0) are included, and since I need all of them, I removed Include= in Controller and added more HiddenFor fields in the View. If I haven't had done this, Controller would attempt to change initial values to null) or 0.Final code:My Controller:[HttpPost, ActionName("SaveChanges")]public ActionResult SaveChanges([Bind(Prefix="MemberOrder")] CustomerOrderLine line) // whitelist fields{ if(ModelState.IsValid) { db.Entry(line).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("CustomerOrder"); } return View(line);}我的看法:@using (Html.BeginForm("SaveChanges", "CustomerOrder", new { id = MemberOrder.customerOrderLineID })){ @Html.ValidationSummary(true) @Html.HiddenFor(model => MemberOrder.customerOrderLineID) @Html.HiddenFor(model => MemberOrder.customerOrderID) @Html.HiddenFor(model => MemberOrder.productID) @Html.HiddenFor(model => MemberOrder.customerOrderQtyMin) @Html.EditorFor(model => MemberOrder.customerOrderQtyMax) @Html.HiddenFor(model => MemberOrder.customerOrderQtyActual) @Html.ValidationMessageFor(model => MemberOrder.customerOrderQtyMax) <input type="submit" value="+" class="btn btn-default" />}有关类似问题的更多资源:ASP.NET MVC Model Binding - Part1 (CodeProject)How to use Bind Prefix? (StackOverflow)ASP.Net MVC3 Model Binding IEnumerable with Editor Template (StackOverflow)How to bind multiple prefixes? (ASP.NET)Multiple submit buttons with ASP.NET MVC: final solution (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 此处的关键问题是for循环会弄乱已发布的变量名,从而使自动绑定失败。因此,您需要将Prefix参数添加到Bind中。[Bind(Prefix="MemberOrder", ...)]您在Editor中为您的视图中的助手指定的前缀:@Html.EditorFor(model => MemberOrder.customerOrderQtyMax)此外,将行ID添加到“绑定包含”。[HttpPost, ActionName("SaveChanges")]public ActionResult SaveChanges( [Bind(Prefix="MemberOrder", Include = "customerOrderLineID,customerOrderQtyMax")] SNW.Models.CustomerOrderInfo.MemberOrder line){ if(ModelState.IsValid) { db.Entry(line).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("CustomerOrder"); } return View(line);} (adsbygoogle = window.adsbygoogle || []).push({}); 10-08 08:01