我是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(參見上文)。但是,當我更改下拉列表選擇時,沒有任何反應。 –