![](/img/trans.png)
[英]Set required to select validation for dropdown list in asp.net razor
[英]Dropdown list with Razor instead of <select> with HTML
我有一個這樣的選擇清單
<select id="select-product">
<option>Select a Product</option>
</select>
我在運行時使用Ajax填寫了此列表,現在我想將其轉換為剃刀視圖,並且在重定向到頁面之前,我會將數據包含在Viewbag中。
我怎樣才能做到這一點 ?
在重定向到頁面之前,我將數據包含在Viewbag中。
一種常見的(也是很好的)方法是在視圖中使用@Html.DropDownList()
或@Html.DropDownListFor<>()
。 但是,您應該在ViewBag
使用強類型模型而不是結構較少的數據。
也可以看看:
$(document).ready(function () {
$.get('/Home/GetProducts/' + $(this).val(), function (response) {
var products = $.evalJSON(response);
var ddlSelectedProduct = $("#SelectedProduct");
// clear all previous options
$("#SelectedProduct > option").remove();
// populate the products
for (i = 0; i < products.length; i++) {
ddlSelectedProduct.append($("<option />").val(products[i].Value).text(products[i].Text));
}
});
});
要么
@Html.DropDownListFor(
x => x.SelectedProductId,
new SelectList(ViewBag.Products as SelectList, "Value", "Text"),
"-- Select Product--"
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.