我发誓我有一段时间试图从静态状态列表中填充DropDownList。我希望通过EntityFramework从db预先填充此列表。
我的RegisterViewModel
public class RegisterViewModel
{
[Required(ErrorMessage = "You can't leave this empty")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[Display(Name = "User name")]
[RegularExpression(@"^[a-zA-Z0-9]*$", ErrorMessage = "The username can only be comprised of letters.")]
public string UserName { get; set; }
[Required(ErrorMessage = "You can't leave this empty")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "You can't leave this empty")]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The Password and Confirmed Password don't match.")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "You can't leave this empty")]
[EmailAddress(ErrorMessage = "Enter a valid E-mail Address")]
[Display(Name = "Email Address")]
public string Email { get; set; }
[Required(ErrorMessage = "You can't leave this empty")]
[EmailAddress(ErrorMessage = "Enter a valid E-mail Address")]
[Display(Name = "Confirm Email")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The Email Address and Confirmed Email don't match")]
public string ConfirmEmail { get; set; }
[Required(ErrorMessage = "You can't leave this empty")]
[Display(Name = "First name")]
[RegularExpression(@"^[a-zA-Z\s]*$", ErrorMessage = "First name can only be comprised of letters.")]
public string FirstName { get; set; }
[Display(Name = "Last name")]
[RegularExpression(@"^[a-zA-Z0-9\s]*$", ErrorMessage = "Last name can only be comprised of letters.")]
public string LastName { get; set; }
public string Gender { get; set; }
public string State { get; set; }
[Display(Name = "Date Of Birth")]
[Required(ErrorMessage = "You can't leave this empty")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateOfBirth { get; set; }
public string Address { get; set; }
public int ZipCode { get; set; }
public string Phone { get; set; }
}
国家的静态清单?
public static class StaticLists
{
public static List<SelectListItem> States = new List<SelectListItem>()
{
new SelectListItem() {Text = "Alabama", Value = "AL"},
new SelectListItem() {Text = "Alaska", Value = "AK"},
new SelectListItem() {Text = "Arizona", Value = "AZ"},
new SelectListItem() {Text = "Arkansas", Value = "AR"},
new SelectListItem() {Text = "California", Value = "CA"},
new SelectListItem() {Text = "Colorado", Value = "CO"},
new SelectListItem() {Text = "Connecticut", Value = "CT"},
new SelectListItem() {Text = "District of Columbia", Value = "DC"},
new SelectListItem() {Text = "Delaware", Value = "DE"},
new SelectListItem() {Text = "Florida", Value = "FL"},
new SelectListItem() {Text = "Georgia", Value = "GA"},
new SelectListItem() {Text = "Hawaii", Value = "HI"},
new SelectListItem() {Text = "Idaho", Value = "ID"},
new SelectListItem() {Text = "Illinois", Value = "IL"},
new SelectListItem() {Text = "Indiana", Value = "IN"},
new SelectListItem() {Text = "Iowa", Value = "IA"},
new SelectListItem() {Text = "Kansas", Value = "KS"},
new SelectListItem() {Text = "Kentucky", Value = "KY"},
new SelectListItem() {Text = "Louisiana", Value = "LA"},
new SelectListItem() {Text = "Maine", Value = "ME"},
new SelectListItem() {Text = "Maryland", Value = "MD"},
new SelectListItem() {Text = "Massachusetts", Value = "MA"},
new SelectListItem() {Text = "Michigan", Value = "MI"},
new SelectListItem() {Text = "Minnesota", Value = "MN"},
new SelectListItem() {Text = "Mississippi", Value = "MS"},
new SelectListItem() {Text = "Missouri", Value = "MO"},
new SelectListItem() {Text = "Montana", Value = "MT"},
new SelectListItem() {Text = "Nebraska", Value = "NE"},
new SelectListItem() {Text = "Nevada", Value = "NV"},
new SelectListItem() {Text = "New Hampshire", Value = "NH"},
new SelectListItem() {Text = "New Jersey", Value = "NJ"},
new SelectListItem() {Text = "New Mexico", Value = "NM"},
new SelectListItem() {Text = "New York", Value = "NY"},
new SelectListItem() {Text = "North Carolina", Value = "NC"},
new SelectListItem() {Text = "North Dakota", Value = "ND"},
new SelectListItem() {Text = "Ohio", Value = "OH"},
new SelectListItem() {Text = "Oklahoma", Value = "OK"},
new SelectListItem() {Text = "Oregon", Value = "OR"},
new SelectListItem() {Text = "Pennsylvania", Value = "PA"},
new SelectListItem() {Text = "Rhode Island", Value = "RI"},
new SelectListItem() {Text = "South Carolina", Value = "SC"},
new SelectListItem() {Text = "South Dakota", Value = "SD"},
new SelectListItem() {Text = "Tennessee", Value = "TN"},
new SelectListItem() {Text = "Texas", Value = "TX"},
new SelectListItem() {Text = "Utah", Value = "UT"},
new SelectListItem() {Text = "Vermont", Value = "VT"},
new SelectListItem() {Text = "Virginia", Value = "VA"},
new SelectListItem() {Text = "Washington", Value = "WA"},
new SelectListItem() {Text = "West Virginia", Value = "WV"},
new SelectListItem() {Text = "Wisconsin", Value = "WI"},
new SelectListItem() {Text = "Wyoming", Value = "WY"}
};
}
我正在考虑创建一个名为“静态列表”的类,但现在我认为将它作为RegisterViewModel中的属性更好?
我可以合并以下内容吗?
public List<ListOfStates> State {get;set;}
public List<string> ListOfStates {get;set;}
我尝试从Controller传递一个ViewBag项目,但我不是很成功......我现在看不到任何内容
public ActionResult Register()
{
ViewBag.ListOfStates = StaticLists.States;
return View();
}
这就是视图的样子,这又是我的View的样子,再次,无法弄清楚如何填充列表......
@Html.DropDownListFor(m => m.State, ViewBag.ListOfStates)
获得一些运行时错误
"as no applicable method named DropDownListFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched....."
答案 0 :(得分:5)
解决了它,如果有人遇到同样的问题。
静态类
public static IEnumerable<SelectListItem> StateListItems()
{
var items = new List<SelectListItem>
{
new SelectListItem() {Text = "Alabama", Value = "AL"},
new SelectListItem() {Text = "Alaska", Value = "AK"},
new SelectListItem() {Text = "Arizona", Value = "AZ"},
new SelectListItem() {Text = "Arkansas", Value = "AR"},
new SelectListItem() {Text = "California", Value = "CA"},
new SelectListItem() {Text = "Colorado", Value = "CO"},
new SelectListItem() {Text = "Connecticut", Value = "CT"},
new SelectListItem() {Text = "District of Columbia", Value = "DC"},
new SelectListItem() {Text = "Delaware", Value = "DE"},
new SelectListItem() {Text = "Florida", Value = "FL"},
new SelectListItem() {Text = "Georgia", Value = "GA"},
new SelectListItem() {Text = "Hawaii", Value = "HI"},
new SelectListItem() {Text = "Idaho", Value = "ID"},
new SelectListItem() {Text = "Illinois", Value = "IL"},
new SelectListItem() {Text = "Indiana", Value = "IN"},
new SelectListItem() {Text = "Iowa", Value = "IA"},
new SelectListItem() {Text = "Kansas", Value = "KS"},
new SelectListItem() {Text = "Kentucky", Value = "KY"},
new SelectListItem() {Text = "Louisiana", Value = "LA"},
new SelectListItem() {Text = "Maine", Value = "ME"},
new SelectListItem() {Text = "Maryland", Value = "MD"},
new SelectListItem() {Text = "Massachusetts", Value = "MA"},
new SelectListItem() {Text = "Michigan", Value = "MI"},
new SelectListItem() {Text = "Minnesota", Value = "MN"},
new SelectListItem() {Text = "Mississippi", Value = "MS"},
new SelectListItem() {Text = "Missouri", Value = "MO"},
new SelectListItem() {Text = "Montana", Value = "MT"},
new SelectListItem() {Text = "Nebraska", Value = "NE"},
new SelectListItem() {Text = "Nevada", Value = "NV"},
new SelectListItem() {Text = "New Hampshire", Value = "NH"},
new SelectListItem() {Text = "New Jersey", Value = "NJ"},
new SelectListItem() {Text = "New Mexico", Value = "NM"},
new SelectListItem() {Text = "New York", Value = "NY"},
new SelectListItem() {Text = "North Carolina", Value = "NC"},
new SelectListItem() {Text = "North Dakota", Value = "ND"},
new SelectListItem() {Text = "Ohio", Value = "OH"},
new SelectListItem() {Text = "Oklahoma", Value = "OK"},
new SelectListItem() {Text = "Oregon", Value = "OR"},
new SelectListItem() {Text = "Pennsylvania", Value = "PA"},
new SelectListItem() {Text = "Rhode Island", Value = "RI"},
new SelectListItem() {Text = "South Carolina", Value = "SC"},
new SelectListItem() {Text = "South Dakota", Value = "SD"},
new SelectListItem() {Text = "Tennessee", Value = "TN"},
new SelectListItem() {Text = "Texas", Value = "TX"},
new SelectListItem() {Text = "Utah", Value = "UT"},
new SelectListItem() {Text = "Vermont", Value = "VT"},
new SelectListItem() {Text = "Virginia", Value = "VA"},
new SelectListItem() {Text = "Washington", Value = "WA"},
new SelectListItem() {Text = "West Virginia", Value = "WV"},
new SelectListItem() {Text = "Wisconsin", Value = "WI"},
new SelectListItem() {Text = "Wyoming", Value = "WY"}
};
return items;
}
在我看来
@Html.DropDownListFor(m => m.State, SP.BLL.StaticLists.StateListItems(), "Select Your State", new { @class = "form-control" })
答案 1 :(得分:2)
您是否尝试将其传递给View Model而不是ViewBag?你可以这样做:
@Html.DropDownListFor(m => m.State, Model.ListOfStates)
增加:
您的问题是您正在形成错误的静态属性。它应该是这样的:
public static List<SelectListItem> States
{
get
{
return new List<SelectListItem>() {
new SelectListItem() {Text = "Alabama", Value = "AL"},
new SelectListItem() {Text = "Alaska", Value = "AK"},
new SelectListItem() {Text = "Arizona", Value = "AZ"},