ASP.NET MVC中為DropDownListFor設定選中項的方法

來源:互聯網
上載者:User

標籤:項目   var   擷取   傳遞   item   資料庫   資料   context   order   

在MVC中,當涉及到強型別編輯頁,如果有select元素,需要根據當前Model的某個屬性值,讓Select的某項選中。本篇只整理思路,不涉及完整代碼。

□ 思路

往前台視圖傳的類型是List<SelectListItem>,把SelectListItem選中項的Selected屬性設定為true,再把該類型對象執行個體放到ViewBag,ViewData或Model中傳遞給前台視圖。

  通過遍曆List<SelectListItem>類型對象執行個體

□ 控制器

?
123456789101112131415161718192021222324 public ActionResult SomeAction(int id){  //從資料庫擷取Domain Model  var domainModel = ModelService.LoadEntities(m => m.ID == id).FirstOrDefault<Model>();    //通過某個方法擷取List<SelectListItem>類型對象執行個體  List<SelectListItem> items = SomeMethod();    //遍曆集合,如果當前Domain model的某個屬性與SelectListItem的Value屬性相等,把SelectListItem的Selected屬性設定為true  foreach(SelectListItem item in items)  {    if(item.Value == Convert.ToString(domainModel.某屬性))    {      item.Selected = true;    }  }    //把List<SelectListItem>集合對象執行個體放到ViewData中  ViewData["somekey"] = items;    //可能涉及到把Domain Model轉換成View Model    return PartialView(domainModel);}

□ 前台視圖顯示

@model DomainModel 
@Html.DropDownListFor(m => m.SomeProperty,(List<SelectListItem>)ViewData["somekey"],"==請選擇==")

通過遍曆Model集合

給View Model設定一個bool類型的欄位,描述是否被選中。 
把Model的某些屬性作為SelectListItem的Text和Value值。根據View Model中的布爾屬性判斷是否要把SelectListItem的Selected設定為true.

□ View Model

?
123456 public class Department{  public int Id {get;set;}  public string Name {get;set;}  public bool IsSelected {get;set;}}

□ 控制器

?
12345678910111213141516171819 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();}

下面是其它網友的補充:

後台代碼:

?
1234567891011121314 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);//根據返回的選中項值設定選中項          ViewData["ruturned"] = collection["drop"];       }       ViewData["drop"] = selec;      return View();    }

前端代碼:

  @using (Html.BeginForm()){
@Html.DropDownList("drop", ViewData["d"] as SelectList)
    <input  type="submit" value="查看對應分組列表" />
        }
        <p> 當前項目ID: @ViewData["ruturned"]</p>

ASP.NET MVC中為DropDownListFor設定選中項的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.