我有一个管理员站点供管理员创建用户。在这里,他必须为用户选择角色 - 比如Asp.NET配置站点。我制作了3个不同角色的复选框。
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
var Rolemodel = model.RolesContainer;
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
[Authorize(Roles = "Admin")]
public ActionResult Register()
{
List<SelectListItem> tempRoles = new List<SelectListItem>();
tempRoles.Add(new SelectListItem{ Text = "Admin", Selected = false, Value = "Admin" });
tempRoles.Add(new SelectListItem{ Text = "Production", Selected = false, Value = "Production"});
tempRoles.Add(new SelectListItem{ Text = "Sale", Selected = false, Value = "Sale"});
return View(new RegisterModel { RolesContainer = tempRoles });
}
---- --------查看
@{ foreach (var item in Model.RolesContainer)
{
@Html.DisplayFor(m => item.Text)
@Html.CheckBoxFor(m => item.Selected)
}
}
当我检查并提交时,我会在Register
操作中找到断点,但此时RolesContainer
为空 - 有人可以告诉我为什么会这样吗?
答案 0 :(得分:2)
我将您的代码(cshtml除外)复制并粘贴到以下文件中,并按预期工作。
<强> HomeController.cs 强>
using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<SelectListItem> tempRoles = new List<SelectListItem>();
tempRoles.Add(new SelectListItem { Text = "Admin",
Selected = false,
Value = "Admin" });
tempRoles.Add(new SelectListItem { Text = "Production",
Selected = false,
Value = "Production" });
tempRoles.Add(new SelectListItem { Text = "Sale",
Selected = false,
Value = "Sale" });
return View(new RegisterModel { RolesContainer = tempRoles });
}
}
}
<强> RegisterModel 强>
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MvcApplication1.Models
{
public class RegisterModel
{
[Required]
[Display(Name = "Brugernavn")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email adresse")]
public string Email { get; set; }
public List<SelectListItem> RolesContainer { get; set;}
}
}
主/ Index.cshtml 强>
@model MvcApplication1.Models.RegisterModel
@{
foreach (var item in Model.RolesContainer)
{
@Html.DisplayFor(m => item.Text)
@Html.CheckBoxFor(m => item.Selected)
}
}
您的Register.cshtml文件中很可能出现了问题,我们无需对其进行验证。
答案 1 :(得分:0)
目前,当您将值分配给RolesContainer
时,它尚未初始化。您只需要在类的构造函数中初始化它:
public class RegisterModel
{
public RegisterModel() {
RolesContainer = new List<SelectListItem>;
}
// rest of your code
}
或者你可以创建一个接受List<>
作为参数的构造函数,但上面的方法适用于你当前的代码结构。
答案 2 :(得分:0)
如果我理解得很好(可能我完全错了),您发布到httppost寄存器(RegisterModel)的模型不包含您期望的RolesContainer。我认为,asp.net mvc无法将多个复选框与List<SelectListItem>
匹配,ListBox应该更好