我正在使用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)'有一些無效參數
YESSS現在的頁面加載!但它似乎在選項選擇框中顯示system.web.webpages.html.selectlist項目。 – user3317526
沒有得到你嗎?你還在犯錯誤嗎?你可以更新你的問題,具體更多細節。 – SBirthare