我是MVC和jQuery的新手。以下问题涉及两个表,Product和QuoteDetail。我正在使用DropDownListFor来允许用户根据Name从Product表中选择产品。一旦选择了Name,QuoteDetail记录中的两个文本框分别用ProductId和Price填充。一切都很好。我的问题是,当我保存QuoteDetail记录时,它将ProductId保存在ProductName字段而不是ProductName中。我没有看到我如何摆脱在SelectList中使用ProductId,因为我需要它来执行查找。如何解决此问题并保存ProductName?
这是DropDownListFor读取的方式:
@Html.DropDownListFor(model => model.ProductName, new SelectList(ViewBag.ProductData, "ProductId", "Name"), new { htmlAttributes = new { @id = "ProductName", @class = "ProductList" } });
查找的jQuery如下:
<script type="text/javascript">
$(document).ready(function () {
$(document).on("change", '[id*="ProductName"]', function () {
$.post("/QuoteViewModel/GetProduct", { pId: $(this).val() }, function (data) {
$("[id*='ProductId']").val(data.ProductId);
$("[id*='Price']").val(data.Price);
});
});
});
</script>
,查找的控制器操作如下:
[HttpPost]
public ActionResult GetProduct(int pId)
{
var data = db.Products.Find(pId);
return Json(data);
}
请注意,产品名称在Product表中称为“Name”,在QuoteDetail表中称为“ProductName”。任何帮助将不胜感激。
用于保存的代码如下:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(QuoteViewModel qvm,[Bind(Include = "CustomerId,SalesRep,FirstName,LastName,Company,Address1,Address2,City,State,PostalCode,WorkPhone,CellPhone,Email,Discount,PaymentTerms")] Customer customer,
[Bind(Include = "QuoteId,QuoteDetailId,ProductId,ProductName,Amount,ListPrice,Discount,Price")] List<QuoteDetail> quoteDetails,
[Bind(Include = "QuoteId,CustomerId,Subtotal,Tax,Total,QuoteDate,GoodUntil,QuoteSent,DateApproved,DateOrdered")] Quote quote)
{
if (ModelState.IsValid)
{
foreach (var quoteDetail in qvm.QuoteDetail)
{
QuoteDetail qd = db.QuoteDetails.FirstOrDefault(o => ((o.QuoteId == quoteDetail.QuoteId) && (o.QuoteDetailId == quoteDetail.QuoteDetailId)));
if (qd != null)
{
qd.ProductId = quoteDetail.ProductId;
qd.ProductName = quoteDetail.ProductName;
qd.Amount = quoteDetail.Amount;
qd.ListPrice = quoteDetail.ListPrice;
qd.Discount = quoteDetail.Discount;
qd.Price = quoteDetail.Price;
}
else
{
db.QuoteDetails.Add(quoteDetail);
}
}
quote.QuoteId = qvm.QuoteId;
quote.GoodUntil = qvm.GoodUntil;
quote.Discount = qvm.Discount;
quote.DateApproved = qvm.DateApproved;
quote.DateOrdered = qvm.DateOrdered;
quote.Discount = qvm.Discount;
quote.QuoteDate = qvm.QuoteDate;
quote.QuoteSent = qvm.QuoteSent;
quote.Subtotal = qvm.Subtotal;
quote.Tax = qvm.Tax;
quote.Total = qvm.Total;
customer.CustomerId = qvm.CustomerId;
customer.Address1 = qvm.Address1;
customer.Address2 = qvm.Address2;
customer.CellPhone = qvm.CellPhone;
customer.City = qvm.City;
customer.Company = qvm.Company;
customer.Discount = qvm.Discount;
customer.Email = qvm.Email;
customer.FirstName = qvm.FirstName;
customer.LastName = qvm.LastName;
customer.PaymentTerms = qvm.PaymentTerms;
customer.PostalCode = qvm.PostalCode;
customer.SalesRep = qvm.SalesRep;
customer.State = qvm.State;
customer.WorkPhone = qvm.WorkPhone;
db.Entry(quote).State = EntityState.Modified;
db.Entry(customer).State = EntityState.Modified;
bool saveFailed;
do
{
saveFailed = false;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
var objContext = ((IObjectContextAdapter)db).ObjectContext;
// Get failed entry
var entry = ex.Entries.Single();
// Now call refresh on ObjectContext
objContext.Refresh(RefreshMode.ClientWins, entry.Entity);
}
} while (saveFailed);
return RedirectToAction("Index");
}
return View(qvm);
}
}
}
修订DropDownListFor:
@Html.DropDownListFor(model => model.ProductName, new SelectList(ViewBag.ProductData, "ProductId", "Name"), new { htmlAttributes = new { name = "ProductId", @id = "ProductName", @class = "ProductList" } });
答案 0 :(得分:1)
这是因为当您提交表单时,所有相应表单元素中的值都会映射到包含它们的元素的name
属性,而不是id
属性。
您需要确保在元素上按预期显式设置name
属性,以确保它们绑定了正确的值:
<!-- If you wanted the value from this bound as "ProductId", you would need to set the
name attribute in the htmlAttributes object -->
@Html.DropDownListFor(model => model.ProductName, ..., new { ... name="ProductId" } });
根据您的需要,可能更容易根本不对DropDownList进行任何更改,并允许它直接绑定到您的ProductId属性,然后在AJAX调用完成后设置您的名字:
@Html.DropDownListFor(model => model.ProductId, new SelectList(ViewBag.ProductData, "ProductId", "Name"), new { @class = "ProductList" } });
以及:
$(document).on("change", '[id*="ProductId"]', function () {
$.post("/QuoteViewModel/GetProduct", { pId: $(this).val() }, function (data) {
$("[id*='ProductId']").val(data.ProductId);
// This could be a hidden element within your form
$("[id*='ProductName']").val(data.ProductName);
$("[id*='Price']").val(data.Price);
});
});