2013-03-31 40 views
0

//此代碼thows了一個錯誤,有型的沒有可視數據項// IEnumerable的用於Companytype我怎麼能在寄存器模型添加一個下拉列表

//當前代碼在RegisterModel

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; } 






} 

//當前代碼鑑於

 <li> 
      @Html.LabelFor(m => m.UserName) 
      @Html.TextBoxFor(m => m.UserName) 
     </li> 

     <li> 
      @Html.LabelFor(m => m.Email) 
      @Html.TextBoxFor(m => m.Email) 
     </li> 

     <li> 
      @Html.LabelFor(m => m.CompName) 
      @Html.TextBoxFor(m => m.CompName) 
     </li> 

     <li> 
      @Html.LabelFor(m => m.CompTypeList) 
      @Html.DropDownList("Companytype","-select one-") 
     </li> 

     <li> 
      @Html.LabelFor(m => m.TotalBranches) 
      @Html.TextBoxFor(m => m.TotalBranches) 
     </li> 




     <li> 
      @Html.LabelFor(m => m.Password) 
      @Html.PasswordFor(m => m.Password) 
     </li> 
     <li> 
      @Html.LabelFor(m => m.ConfirmPassword) 
      @Html.PasswordFor(m => m.ConfirmPassword) 
     </li> 

//的AccountController註冊 私人DefaultDBEntities3分貝=新DefaultDBEntities3();

[AllowAnonymous] 
    public ActionResult Register() 
    { 
     ViewBag.CompTypeList = new SelectList("Companytype"); 
     return View(); 
    } 

    protected override void Dispose(bool disposing) 
    { 

     db.Dispose(); 
     base.Dispose(disposing); 
    } 

你的幫助,我將非常感激

+0

//數據庫名DefaultDB // DBTABLE CoAdd // DBvar COID(標識)和Companytype 插入CoAdd值( '電信') INSERT INTO CoAdd值( '汽車') INSERT INTO CoAdd值(」政府') 插入到CoAdd值中('Banking' – vash363

+0

您不需要將模型下拉列表添加到模型中,它們是在視圖中創建的,我想您要問的是,什麼樣的實體會支撐下拉列表。研究。這個論壇不是針對那種問題的。 –

回答

0

選擇列表項是針對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是要顯示的字段。希望這可以幫助。

相關問題