Sql server 2008 如何在寄存器模型中添加dropdownlist
//此代码导致一个错误,即Companytype没有//IEnumerable类型的viewdata项 //RegisterModel中的当前代码Sql server 2008 如何在寄存器模型中添加dropdownlist,sql-server-2008,asp.net-mvc-4,Sql Server 2008,Asp.net Mvc 4,//此代码导致一个错误,即Companytype没有//IEnumerable类型的viewdata项 //RegisterModel中的当前代码 public class RegisterModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [Display(Name = "Email Address")]
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "Email Address")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[Display(Name = "Company Name")]
public string CompName { get; set; }
[Required]
[Display(Name = "Company Type")]
public IEnumerable<SelectListItem> CompTypeList { get; set; }
[Required]
[Display(Name = "Total number of Branches")]
[Range(1,10,ErrorMessage = "The {0} must be at least {1}")]
public int TotalBranches { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
非常感谢您的帮助对于html.dropdownlist帮助程序,选择列表项是无效的参数。它正在寻找一份清单。仅在viewbag中存储数据,并在视图中创建选择列表,如下所示: //控制器
public ActionResult Register()
{
ViewBag.CompTypeList = db.Companytype.OrderBy(y => y.Name).ToList();
return View();
}
//看法
@Html.DropDownList("CompanyType", new SelectList(ViewBag.CompTypeList, "Id", "Name"))
这将正确地呈现视图中的下拉列表,并消除错误。Id是列表的值字段,Name是要显示的字段。希望这有帮助 //DBName DefaultDB//DBTable CoAdd//DBvar CoID(标识)和Companytype插入CoAdd值(“电信”)插入CoAdd值(“汽车”)插入CoAdd值(“政府”)插入CoAdd值(“银行业”你不会在模型中添加dropdownlist。它们是在视图中创建的。我想你要问的是,什么样的实体会构建dropdownlist。这是你应该研究的。这个论坛不是为这类问题而设的。
public ActionResult Register()
{
ViewBag.CompTypeList = db.Companytype.OrderBy(y => y.Name).ToList();
return View();
}
@Html.DropDownList("CompanyType", new SelectList(ViewBag.CompTypeList, "Id", "Name"))