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.
public SelectList( IEnumerable items, string dataValueField, string dataTextField, Object selectedValue )
使用这个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的第一项默认选择项