[英]How to get fieldname from SQL Server DB using the field ID
在我看來,我有一些下拉列表,可用來獲取ID並向用戶顯示值,以便他們知道自己在選擇什么。 但是,在我用來列出已添加的所有內容的視圖中,它顯示了所選的ID,但我希望顯示該名稱
我嘗試了此操作,但它始終顯示ID為1的狀態,即狀態為“有效”
@Html.DropDownListFor(modelItem => item.status,
(SelectList)ViewBag.listStatus,
new { @class disabled = "" })
//控制器
{
public ActionResult listStatus()
{
var list= ListsDAO.listStatus();
return View(list);
}
}
// DAO
{
public static IEnumerable<LISTS> listStatus()
{
using (var ctx = new PadraoEntities())
{
var result= from lists in ctx.LISTS
where lists.TYPE_LIST== "STATUS"
select new
{
lists.IDE_LIST,
lists.DES_LIST,
lists.VLR_LIST
};
var list= new List<LISTS>();
foreach (var item in result)
{
list.Add(new LISTS()
{
IDE_LIST = item.IDE_LIST,
VLR_LIST = item.VLR_LIST,
DES_LIST = item.DES_LIST
});
}
return list;
}
}
}
//查看添加
<div class="form-group">
@Html.LabelFor(model => model.status, htmlAttributes: new { @class = "control -Label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.status(SelectList)ViewBag.listStatus, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.status, "", new { @class = "text-danger" })
</div>
</div>
//查看清單
{
@Html.DropDownListFor(modelItem => item.ststus,(SelectList)ViewBag.listStatus,new { disabled = "" })
}
列出ID 1時,我希望顯示“活動”,ID 2顯示“無效”,依此類推
試試這個//控制器
public ActionResult listStatus()
{
ViewBag.Status = new SelectList(//Code to get id and its property, "StatusId", "Status");
var list= ListsDAO.listStatus();
return View(list);
}
//視圖
<div class="form-group">
@Html.LabelFor(model => model.Status, "TaskId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Status", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.