我对asp.net MVC 完全陌生,我从数据库中填充了一个下拉列表。现在我需要在控制器的 get 函数中发送选定的值(id),以便我可以加载选定的值详细信息这是我尝试过的代码。
看法:
@Html.DropDownListFor(model => model.ProductName, new SelectList(Model.ProductsList, "ProductID", "ProductName"), new
{
id = "Productslist",
@class = "GreenDropDownListStyle",
@datastyle = "dropdown",
})
java脚本代码:
<script type="text/javascript">
$(document).ready(function () {
$("#Productslist").change(function () {
debugger;
var SelectedProductID = this.value;
if (SelectedProductID != "") {
$.ajax(
{
type: 'GET',
url: '/Inventory/ViewProduct',
data: { ProductID: SelectedProductID },
contentType: 'application/json; charset=utf-8',
success: function (details) {
//alert("success");
},
});
} else {
}
});
});
</script>
此代码可以获取选定的 id 并查看成功警报消息,但它不会将选定的值 (id) 发送到控制器中的 post 函数
控制器:
[HttpPost]
public ActionResult ViewProduct(int? ProductID)
{
if (ProductID != null && ProductID >0)
{
}
else
{
message = "";
}
return View();
}
试试这个:
var jsn = { ProductID: SelectedProductID };
jsn = JSON.stringify(jsn);
$.ajax({
type: "POST",
url: "/Inventory/ViewProduct",
data: jsn,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function () {
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句