2014-04-26 17 views
0

我正在使用ASP.NET web應用程序模板,並試圖讓用戶在註冊時選擇一個角色。用html選擇框改變角色asp.net mvc4

這是我現在得到的。

查看

<fieldset class="col-lg-5 .col-md-5"> 
     <legend>Registration Form</legend> 
     <p> 
      @Html.LabelFor(m => m.UserName) 
      @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) 
     </p> 
     <p> 
      @Html.LabelFor(m => m.Password) 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
     </p> 
     <p> 
      @Html.LabelFor(m => m.ConfirmPassword) 
      @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
     </p> 
     <p> 
      @Html.DropDownListFor(model => model.Type, Model.TypeList) 
     </p> 

     <input type="submit" value="Register" class="btn btn-default" /> 
    </fieldset> 

型號

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    public string UserName { 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")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    public string Type { get; set; } 

    public IEnumerable<SelectListItem> TypeList 
    { 
     get 
     { 
      return new List<SelectListItem> 
    { 
     new SelectListItem { Text = "athlete", Value = "athlete"}, 
     new SelectListItem { Text = "coach", Value = "coach"}, 
    }; 
     } 
    } 

} 

控制器

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult Register(RegisterModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      // Attempt to register the user 
      try 
      { 
       WebSecurity.CreateUserAndAccount(model.UserName, model.Password); 
       Roles.AddUserToRole(model.UserName, model.Type); 
       WebSecurity.Login(model.UserName, model.Password); 

       return RedirectToAction("Index", "Home"); 
      } 
      catch (MembershipCreateUserException e) 
      { 
       ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

我覺得我接近,但我不能安靜得到它,我得到這個錯誤

編譯器錯誤消息:Models.RegisterModel>'不包含'DropDownListFor'的定義和最佳擴展方法重載'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System .Linq.Expressions.Expression>,System.Collections.Generic.IEnumerable)'有一些無效參數

回答

0

你是否在HttpGet Register方法中初始化模型?像下面...

[AllowAnonymous] 
    public ActionResult Register() 
    { 
     var model = new RegisterModel(); 
     return View(model); 
    } 

我創建一個空的模板MVC4應用程序,添加代碼,得到對象引用未設置錯誤的默認註冊模式不傳遞模型對象,查看您試圖訪問(即加載TypeInt在DropDownListFor())。

然後我在Get方法中初始化模型,如上所示。所有的工作都很好,我可以在註冊視圖中選擇一個角色。

檢查是否有幫助。

+0

YESSS現在的頁面加載!但它似乎在選項選擇框中顯示system.web.webpages.html.selectlist項目。 – user3317526

+0

沒有得到你嗎?你還在犯錯誤嗎?你可以更新你的問題,具體更多細節。 – SBirthare

0

去看一下你的最後一個逗號應刪除:

new SelectListItem { Text = "athlete", Value = "athlete"}, 
new SelectListItem { Text = "coach", Value = "coach"}