MVC架构下,关于如何设置DropDownList的默认选中项以及修改后填充以后的设置选中项
2011-05-02 14:36
549 查看
SelectList Constructor (IEnumerable, String, String, Object)
.NET Framework 4
Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, the data text field, and a selected value.
Namespace:
System.Web.Mvc
Assembly:
System.Web.Mvc (in System.Web.Mvc.dll)
C#
public SelectList( IEnumerable items, string dataValueField, string dataTextField, Object selectedValue )
Parameters
items
Type: System.Collections.IEnumerable
The items.
dataValueField
Type: System.String
The data value field.
dataTextField
Type: System.String
The data text field.
selectedValue
Type: System.Object
The selected value.
使用这个SelectList构造函数,第四个参数就是选中项的值:
http://msdn.microsoft.com/en-us/library/dd492553.aspx
先定义自定义用户配置类 (情形1:默认选中项参数为string类型)
[Serializable]
public class ProfileInformation
{
public string SubscriptionType { get; set; }
public static SelectList GetSubscriptionList(String SubscriptionType)
{
List<SelectListItem> subscriptionList = new List<SelectListItem>()
{
new SelectListItem() { Value = "HTML", Text = "Subscribe to HTML Version" },
new SelectListItem() { Value = "Plain", Text = "Subscribe to Plain Text Version" },
new SelectListItem() { Value = "None", Text = "No Thanks" }
};
return new SelectList(subscriptionList, "Value", "Text", SubscriptionType);
}
}
Controller 控制器方法
public ActionResult UserProfile()
{
string id = HttpContext.User.Identity.Name.ToString();
ProfileBase profileBase;
if (!String.IsNullOrEmpty(id))
{
profileBase = ProfileBase.Create(id);
}
else
{
profileBase = HttpContext.Profile as ProfileBase;
}
ProfileInformation profile =( ProfileInformation) profileBase.GetPropertyValue("ProfileInformation") ;
ViewData["subscriptionType"] = ProfileInformation.GetSubscriptionList(profile.SubscriptionType);
if (profile == null)
{
ProfileInformation profileInformation = new ProfileInformation()
{
};
profileBase.SetPropertyValue("ProfileInformation", profileInformation);
}
return View(profile);
}
[AcceptVerbs("POST")]
public ActionResult UserProfile(ProfileInformation profileInformation)
{
ProfileBase profileBase = HttpContext.Profile as ProfileBase;
profileBase.SetPropertyValue("ProfileInformation", profileInformation);
//profileInformation.GenderType = Request.Form["GenderType"];
profileBase.Save(); //通过设置视图页面的强类型为ProfileInformation并从控制器为视图传入一个ProfileInformation类型的值profile,POST操作中可直接保存新的profile值,<%= Html.DropDownList("SubscriptionType")%>中的值将自动匹配ProfileInformation的属性SubscriptionType
ProfileInformation profile =(ProfileInformation) profileBase.GetPropertyValue("ProfileInformation") ;
ViewData["subscriptionType"] = ProfileInformation.GetSubscriptionList(profileInformation.SubscriptionType.ToString());
TempData["SuccessMessage"] = "Your profile information has been saved";
ViewData["PageTitle"] = "My Profile";
return View(profileInformation);
}
标注为红色的为SelectList所设置的选中项所传人的值,若传入的值SubscriptionType为"" ,则SelectList采用默认选择项即第一项,否则采用传入的值SubscriptionType所对应的项为选中项 (注意:string类型不具有可空类型,默认为"",不为null,而int类型可设为可空类型,int?)
View 视图部分 (视图页面设置强类型为YouXieKu.Models.ProfileInformation)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/UserCenter.Master" Inherits="System.Web.Mvc.ViewPage<YouXieKu.Models.ProfileInformation>" %>
<span class="label UserEditlabel">订阅类型:</span>
<div class="fl">
<%= Html.DropDownList("SubscriptionType")%> //绑定键为subscriptionType的ViewData值 也可写作<%= Html.DropDownList("subscriptionType")%> 此处大小写无关 注意:若要设置填充修改后的选中项的话,此处不可写作<%= Html.DropDownList("SubscriptionType",ViewData["subscriptionType"] as SelectList)%>,否则将无法填充修改后的选择项而
是默认绑定SelectList的第一项默认选择项
</div>
静态类静态方法 (情形2: 默认选中项参数为int? 可空类型)
public static SelectList GetDepartmentList(int? department)
{
TreeHelper departmentTree = new TreeHelper();
List<SelectListItem> departmentList = new List<SelectListItem>();
foreach (var item in departmentTree.departments)
{
departmentList.Add(new SelectListItem() { Value = item.DepartmentID.ToString(), Text = item.Title }); // (departmentTree.departments);
}
return new SelectList(departmentList, "Value", "Text",department);
}
Controller 控制器方法
ViewData["departmentID"] = Product.GetDepartmentList(product.DepartmentID);
更方便的生成ViewData["departmentID"]的方式 如下:
ViewData["Categories"] = new SelectList(CategoriesDal.GetAllCategories(), "Id", "Name", art.CategoryId);
View 视图部分
<%= Html.DropDownList("DepartmentID")%> //绑定键为DepartmentID的ViewData值 也可写作<%= Html.DropDownList("departmentID")%> 此处大小写无关注意:若
要设置填充修改后的选中项的话,此处也不可写作<%=
Html.DropDownList("DepartmentID",ViewData["departmentID"] as
SelectList)%>,否则将无法填充修改后的选择项而是默认绑定SelectList的第一项默认选择项
╮(╯▽╰)╭,文章着实很乱,大家有什么疑问可以问我,我会尽量回答
相关文章推荐
- 【转】Asp.net MVC中关于HtmlHelper给DropDownList设置初始选中值的问题
- MVC RC2中关于HtmlHelper给DropDownList设置初始选中值的问题
- 关于如何在XML中设置RadioButton默认选中
- [FAQ04326]如何打开一种语言以及如何设置开机 默认语言
- (实战)Spring Portlet MVC处理请求分析以及如何快速架构一个Portlet项目
- 如何给DropDownList控件设置样式(ASP.NET MVC)
- asp.net MVC中dropdownlist的使用(并设置选中项)
- iOS 关于UITabBarItem 选中图片的颜色以及标题的颜色设置
- HTML中的<select>标签如何设置默认选中的选项
- 如何在CorelDRAW中对轮廓和填充进行默认设置
- HTML中的<select>标签如何设置默认选中的选项
- 表视图默认选中第一个cell以及button外沿特性设置
- 关于imageview 不让背景图片自动拉伸填充 imageview 以及布局参数的设置的小结(自己试验的可能不怎么准确)
- Wpf Datagrid 设置了模板 checkbox 如何一次选中,单击就选中。默认是要双击才得
- 如何使DropdownList 默认选中为从数据库中查询的字段
- 如何打开一种语言以及如何设置开机默认语言
- 关于Tomcat的点点滴滴(体系架构、处理http请求的过程、安装和配置、目录结构、设置压缩和对中文文件名的支持、以及Catalina这个名字的由来……等)
- DropDownList设置默认选中项
- jQuery MultiSelect 如何设置多选框的默认选中项
- 概述struts,以及struts如何实现MVC架构的