我正在創建一個自定義註冊表單,供本網站的管理員在本註冊表單中使用,管理員可以添加用戶並在網站中爲他們提供角色,這個模型從如何從asp.net中的SelectList中獲取項目4
public class AddUserToRoleModels
{
[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; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Select Role")]
public string Roles { get; set; }
}
類功能定義,使這個方法來添加用戶,並給他們的角色
public class Functions
{
public void AddUserToRole(string UserName,string PassWord,string Role) {
if (!WebSecurity.UserExists(UserName))
WebSecurity.CreateUserAndAccount(UserName, PassWord);
if (!Roles.GetRolesForUser(UserName).Contains(Role))
Roles.AddUsersToRoles(new[] { UserName }, new[] { Role });
}
}
然後我創建這個控制器
public class AddUserToRoleController : Controller
{
UsersContext db = new UsersContext();
public ActionResult Create()
{
ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
return View();
}
[HttpPost]
public ActionResult Create(AddUserToRoleModels model)
{
if (ModelState.IsValid)
{
Functions ob = new Functions();
ob.AddUserToRole(model.UserName, model.Password, model.Roles);
return RedirectToAction("Index");
}
ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
return View(model);
}
}
,併爲創建動作控制器
@model SeniorProject.Models.AddUserToRoleModels
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AddUserToRoleModels</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Roles)
</div>
<div class="editor-field">
@Html.DropDownList("RoleId", String.Empty)
@Html.ValidationMessageFor(model => model.Roles)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
的觀點時,我嘗試添加新用戶,並給他角色的用戶插入到數據庫,但沒有作用,我想是因爲我是試圖讓從@Html.DropDownList("RoleId", String.Empty)
,這是清單,我的方法是採取字符串參數的作用,我試圖從這個名單採取選定的項目,但沒有工作乙醚,請幫助作用...
需要將角色ID綁定到一個int領域。在Ryan的回答中,下拉列表值將綁定到模型屬性'int RoleID' – CSharper
您的代碼編寫方式,如果您將'int RoleId'添加到Create方法的參數列表中,它將綁定到該屬性。 – CSharper
在創建httppost操作中,角色ID是否與模型一起發佈? –