我有專案的課
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
然后,它使用下拉串列顯示
@Html.DropDownListFor(model => model.Id,
new SelectList(Model.Projects, "Id", "Name", Model.Id),
"-- Select --",
new { @class = "form-control" })
如果我選擇一個專案,我可以獲得Id和Name,但如何獲得描述?
<script>
$(function () {
$('#id').change(function () {
alert('id = ' $('#Id').val() ', name = ' $('#Id :selected').text() ', description = ' ???);
});
});
</script>
uj5u.com熱心網友回復:
有兩種方式
如果專案串列非常大,您將不得不創建 API 并使用 ajax 來獲取描述。
在javascript中從模型中獲取專案串列并找到描述
<script>
var projects = @Html.Raw(Json.Encode(Model.Projects));
console.log(JSON.stringify(projects)); //only for test
$(function () {
$('#id').change(function () {
let id= $('#Id').val();
let description = null;
let name = null;
projects.forEach((item) => {
if (item.Id === id) {
name= item.Name;
description= item.Description;
break;
}
});
alert('id = ' id ', name = ' name ', description = ' description);
});
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311579.html
標籤:C# asp.net-mvc 剃刀
