【发布时间】:2017-02-19 00:42:32
【问题描述】:
我是 MVC 新手,也是 JQuery 新手。我正在尝试根据下拉列表选择填充文本框。我的产品模型包含字段 ProductId、Name 和 Price。我想根据选择的产品名称在我的 QuoteDetails 中填充 ProductId 和 Price 字段。我的控制器动作如下:
public ActionResult AddProduct(int quoteId, int quoteDetailId)
{
var items = db.Products.ToList();
ViewBag.ProductData = items;
ViewData["QuoteId"] = quoteId;
ViewData["QuoteDetailId"] = quoteDetailId;
return PartialView("EditQuoteDetail", new QuoteDetail { QuoteId = quoteId, QuoteDetailId = quoteDetailId, ProductId = 1, ProductName = " ", Amount = 1, ListPrice = 0, Discount = 0, Price = 0 });
}
部分视图EditQuoteDetail的相关部分如下:
@Html.HiddenFor(model => model.QuoteId, new { htmlAttributes = new { @class = "form-control" } })
@Html.HiddenFor(model => model.QuoteDetailId, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.ProductId, new { htmlAttributes = new { @id="ProductId", @class = "form-control" } })
@Html.DropDownList("ProductName", new SelectList(ViewBag.ProductData, "Name", "Name"), new { @id = "ProductName" })
@Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.ListPrice, new { htmlAttributes = new { @id="Price", @class = "form-control" } })
@Html.EditorFor(model => model.Discount, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
我用来尝试填充 ProductId 和 Price 字段的脚本如下:
<script type="text/javascript">
$(document).ready(function () {
$('#ProductName').change(function () {
$('#ProductId').val($(this).val());
$('#Price').val($(this).val());
});
});
</script>
但是当我进行下拉列表选择时,什么也没有发生。我究竟做错了什么?任何帮助将不胜感激。
【问题讨论】:
-
页面上是否存在这些元素 ID?
change处理程序是否已执行? -
我添加了 ID(见上文)。不过,当我更改下拉列表选择时,什么也没有发生。
标签: c# jquery asp.net-mvc