我是MVC的新手,对JQuery非常新颖。我正在尝试根据下拉列表选择填充文本框。我的产品型号包含字段ProductId,Name和Price。我想根据所选的产品名称在我的QuoteDetails中填充ProductId和Price字段。我的控制器操作如下:如何根据下拉列表选择填充文本框mvc
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" } })
我使用的尝试填充产品ID的脚本和价格领域如下: :
<script type="text/javascript">
$(document).ready(function() {
$('#ProductName').change(function() {
$('#ProductId').val($(this).val());
$('#Price').val($(this).val());
});
});
</script>
但是,当我使下拉列表选择,没有任何反应。我究竟做错了什么?任何帮助将非常感激。
这些元素ID是否存在于页面上?是否执行了'change'处理程序? – David
我添加了id(参见上文)。但是,当我更改下拉列表选择时,没有任何反应。 –