最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年10月22日 17:46:14   投稿:mdxy-dxy  
這篇文章主要介紹了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.

□ View Model

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>

相關(guān)文章

最新評(píng)論

若羌县| 洛隆县| 文水县| 盐津县| 宁城县| 哈密市| 新余市| 杭锦旗| 苍溪县| 固阳县| 罗甸县| 莱西市| 麦盖提县| 丁青县| 东港市| 都江堰市| 平邑县| 喜德县| 子长县| 蓬溪县| 四会市| 兴义市| 米脂县| 景泰县| 城口县| 高州市| 通江县| 登封市| 东平县| 辽宁省| 乃东县| 广丰县| 昌图县| 油尖旺区| 五原县| 綦江县| 通州市| 靖远县| 商城县| 潞西市| 商水县|