gpt4 book ai didi

c# - 静态状态列表上的 DropDownListFor

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:51 24 4
gpt4 key购买 nike

我发誓我在尝试从静态状态列表填充 DropDownList 时遇到了麻烦。我更希望通过 EntityFramework 从数据库中预先填充此列表。

我的 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 项目,但我不是很成功......现在在我的 View 中什么都没有

public ActionResult Register()
{
ViewBag.ListOfStates = StaticLists.States;

return View();
}

这又是 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....."

最佳答案

解决了,如果有人遇到同样的问题。

静态类

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" })

关于c# - 静态状态列表上的 DropDownListFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24857326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com