在静态状态列表上使用DropDownListFor
本文关键字:DropDownListFor 列表 静态 状态 | 更新日期: 2023-09-27 18:14:02
我发誓我有一个地狱的时间试图从一个静态列表的状态填充DropDownList。我希望这个列表通过EntityFramework从数据库中预先填充。
我RegisterViewModelpublic 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;}
我试着从控制器传递ViewBag项目,但我不是很成功…
public ActionResult Register()
{
ViewBag.ListOfStates = StaticLists.States;
return 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....."
解决了,如果有人有同样的问题。
静态类
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;
}
In my view
@Html.DropDownListFor(m => m.State, SP.BLL.StaticLists.StateListItems(), "Select Your State", new { @class = "form-control" })
您是否尝试将其传递给视图模型而不是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"},