首页 > 其他分享 >Razor操作Dropdownlist

Razor操作Dropdownlist

时间:2022-12-24 13:00:47浏览次数:48  
标签:ItemID Razor ViewData itemID 操作 CategoryID Dropdownlist

目的:为Dropdownlist赋值,并获得项目选项值itemID

法一:

//项目列表

ViewData["ItemID"] = string.IsNullOrEmpty(itemID) ? "4" : itemID;

var Result = from a in db.Fee_Items

where a.CategoryID == 7   /*7是工作量*/

select new SelectListItem
{
Text = a.ItemName,
Value = a.ItemID
};

ViewData["ItemList"] = Result.Distinct();

调用:

@Html.DropDownList("ItemID", @ViewBag.ItemList as IEnumerable<SelectListItem>)


法二

//当前项目
ViewData["ItemID"] = string.IsNullOrEmpty(itemID) ? "4" : itemID;
//项目列表
ViewData["ItemIist"] = new SelectList(db.Fee_Items.Where(X => X.CategoryID == 1 || X.CategoryID == 2), "ItemID", "ItemName", ViewData["ItemID"]);

调用: @Html.DropDownList("ItemID", ViewData["ItemIist"] as SelectList);

标签:ItemID,Razor,ViewData,itemID,操作,CategoryID,Dropdownlist
From: https://www.cnblogs.com/Biyuanguang/p/17002768.html

相关文章