[英]How to get another value from the selected dropdownlist
I have project's class我有项目的课
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
then, it's displayed using dropdownlist然后,它使用下拉列表显示
@Html.DropDownListFor(model => model.Id,
new SelectList(Model.Projects, "Id", "Name", Model.Id),
"-- Select --",
new { @class = "form-control" })
If I select a project, I can get the Id and Name, but how to get the description?如果我选择一个项目,我可以获得Id和Name,但如何获得描述?
<script>
$(function () {
$('#id').change(function () {
alert('id = ' + $('#Id').val() + ', name = ' + $('#Id :selected').text() + ', description = ' + ???);
});
});
</script>
There are two ways有两种方式
If list of projects are very large you will have to create API and use ajax to get a description.如果项目列表非常大,您将不得不创建 API 并使用 ajax 来获取描述。
In javascript get list of project from model and find a description在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.