无为清净楼资源网 Design By www.qnjia.com
在MVC中,当涉及到强类型编辑页,如果有select元素,需要根据当前Model的某个属性值,让Select的某项选中。本篇只整理思路,不涉及完整代码。
□ 思路
往前台视图传的类型是List<SelectListItem>,把SelectListItem选中项的Selected属性设置为true,再把该类型对象实例放到ViewBag,ViewData或Model中传递给前台视图。
通过遍历List<SelectListItem>类型对象实例
□ 控制器
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
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 "htmlcode">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>
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年10月04日
2024年10月04日
- 邓丽君.1983-漫步人生路(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 陈容森.1996-情断【上华】【WAV+CUE】
- 裘海正.1994-爱我的人和我爱的人【上华】【WAV+CUE】
- 庾澄庆.1988-错过的爱【福茂】【WAV+CUE】
- 班得瑞原装进口《第十三张新世纪专辑:旭日之丘》1CD[APE/CUE分轨][329.6MB]
- 纯音入心系列纯音乐《古筝|佛蕴|境法禅心意法自然》1CD[MP3][350MB]
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 李国祥.1995-九五变奏【嘉音】【WAV+CUE】
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】