国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.NET MVC中為DropDownListFor設(shè)置選中項(xiàng)的方法

2024-07-10 13:28:18
字體:
供稿:網(wǎng)友
這篇文章主要介紹了ASP.NET MVC中為DropDownListFor設(shè)置選中項(xiàng)的方法,需要的朋友可以參考下
在MVC中,當(dāng)涉及到強(qiáng)類型編輯頁(yè),如果有select元素,需要根據(jù)當(dāng)前Model的某個(gè)屬性值,讓Select的某項(xiàng)選中。本篇只整理思路,不涉及完整代碼。
□ 思路
往前臺(tái)視圖傳的類型是List<SelectListItem>,把SelectListItem選中項(xiàng)的Selected屬性設(shè)置為true,再把該類型對(duì)象實(shí)例放到ViewBag,ViewData或Model中傳遞給前臺(tái)視圖。
  通過遍歷List<SelectListItem>類型對(duì)象實(shí)例
□ 控制器

public ActionResult SomeAction(int id)
{
  //從數(shù)據(jù)庫(kù)獲取Domain Model
  var domainModel = ModelService.LoadEntities(m => m.ID == id).FirstOrDefault<Model>();
 
  //通過某個(gè)方法獲取List<SelectListItem>類型對(duì)象實(shí)例
  List<SelectListItem> items = SomeMethod();
 
  //遍歷集合,如果當(dāng)前Domain model的某個(gè)屬性與SelectListItem的Value屬性相等,把SelectListItem的Selected屬性設(shè)置為true
  foreach(SelectListItem item in items)
  {
    if(item.Value == Convert.ToString(domainModel.某屬性))
    {
      item.Selected = true;
    }
  }
 
  //把List<SelectListItem>集合對(duì)象實(shí)例放到ViewData中
  ViewData["somekey"] = items;
 
  //可能涉及到把Domain Model轉(zhuǎn)換成View Model
 
  return PartialView(domainModel);
}
□ 前臺(tái)視圖顯示
@model DomainModel
@Html.DropDownListFor(m => m.SomeProperty,(List<SelectListItem>)ViewData["somekey"],"==請(qǐng)選擇==")
通過遍歷Model集合
給View Model設(shè)置一個(gè)bool類型的字段,描述是否被選中。
把Model的某些屬性作為SelectListItem的Text和Value值。根據(jù)View Model中的布爾屬性判斷是否要把SelectListItem的Selected設(shè)置為true.

public class Department
{
  public int Id {get;set;}
  public string Name {get;set;}
  public bool IsSelected {get;set;}
}
□ 控制器

public ActionResult Index()
{
SampleDbContext db = new SampleDbContext();
List<SelectListItem> selectListItems = new List<SelectListItem>();
 
//遍歷Department的集合
foreach(Department department in db.Departments)
{
  SelectListItem = new SelectListItem
  {
   Text = department.Name,
   Value = department.Id.ToString(),
   Selected = department.IsSelected.HasValue ? department.IsSelected.Value : false
  }
  selectListItems.Add(selectListItem);
}
ViewBag.Departments = selectListItems;
return View();
}
下面是其它網(wǎng)友的補(bǔ)充:
后臺(tái)代碼:

public ActionResult Index(FormCollection collection)
     {
       IList<Project> li = Utility.SqlHelper.getProjectList();
       SelectList selec = new SelectList(li, "ID", "Name");
   
       if (collection["drop"] != null)
       {
         string projectID = collection["drop"];
         selec = new SelectList(li, "ID", "Name", projectID);//根據(jù)返回的選中項(xiàng)值設(shè)置選中項(xiàng) 
        ViewData["ruturned"] = collection["drop"];
       }
       ViewData["drop"] = selec;
      return View();
    }
前端代碼:
  @using (Html.BeginForm()){
@Html.DropDownList("drop", ViewData["d"] as SelectList)
    <input  type="submit" value="查看對(duì)應(yīng)分組列表" />
        }
        <p> 當(dāng)前項(xiàng)目ID: @ViewData["ruturned"]</p>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 渝中区| 陈巴尔虎旗| 正定县| 株洲市| 阳泉市| 景东| 安平县| 麻栗坡县| 泗水县| 陇西县| 麦盖提县| 哈密市| 苍山县| 日土县| 天长市| 咸宁市| 田林县| 中江县| 盐亭县| 枣强县| 晴隆县| 扶风县| 无锡市| 灵璧县| 荆州市| 左云县| 甘泉县| 电白县| 盱眙县| 黄陵县| 桂东县| 平遥县| 澄迈县| 八宿县| 观塘区| 贵溪市| 曲沃县| 隆安县| 宝坻区| 连城县| 富顺县|