0

我想创建一个与 MVC 4 模板中的不同的 RegisterModel,并且当新用户想要注册时,他们可以选择是否要成为“Premium”。问题是我不知道如何将 IsPremium 的复选框值传递给 WebSecurity.CreateUserAndAccount 方法并使其工作。

问题:当用户想要注册时,有一个 SQL 错误说 IsPremium 不能为空。

问题:我需要修改什么才能使 IsPremium chechbox 的值成为创建用户时插入数据库的值?

我还没有找到关于 RegisterModel 和 User 如何相关的解释,这也很好。

显然,IsPremium 是必需的。

(为简单起见,删除了 al [Display] 和验证注释:

 public class RegisterModel
{
    public string UserName { get; set; }
    public string Correo { get; set; }
    public string  Twitter { get; set; }
    public string LinkedIn { get; set; }
    public string Perfil { get; set; }
    public bool IsPremium { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
}

我的用户类是:

public class Usuario
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    [EmailAddress]
    public string Correo { get; set; }
    public string UserName { get; set; }
    public string Twitter { get; set; }
    public string Perfil { get; set; }
    public bool IsPremium { get; set; }
    public string LinkedIn { get; set; }
    public List<Evento> Ponencias { get; set; }
    public List<Evento> Asistencias { get; set; }
    public List<Evento> EventosCreados { get; set; }
}

创建新帐户的重要视图部分是:

 <fieldset>
    <legend>Formulario de registro</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.UserName)
            @Html.TextBoxFor(m => m.UserName)
        </li>
        <li>
            @Html.LabelFor(m=>m.Correo)
            @Html.TextBoxFor(m=>m.Correo)
        </li>
        <li>
            @Html.LabelFor(m=>m.Twitter)
            @Html.TextBoxFor(m=>m.Twitter)
        </li>
        <li>
            @Html.LabelFor(m=>m.LinkedIn)
            @Html.TextBoxFor(m=>m.LinkedIn)
        </li>
          <li>
            @Html.LabelFor(m=>m.Perfil)
            @Html.TextBoxFor(m=>m.Perfil)
        </li>
        <li>
            @Html.LabelFor(m=>m.IsPremium)
            @Html.CheckBoxFor(m=>m.IsPremium)
        </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>
    </ol>
    <input type="submit" value="Registrarse" />
</fieldset>

最后是我的控制器:

 public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, model.IsPremium); // THIS IS WHERE I GET AN EXCEPTION
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
        return View(model);
    }

非常感谢。

4

1 回答 1

0

这解决了我修改 AccountController 中的注册操作:

public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        try
        {
            WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { IsPremium = model.IsPremium, Twitter= model.Twitter} ); //just add properties to that anonymous object
            WebSecurity.Login(model.UserName, model.Password);
            return RedirectToAction("Index", "Home");
        }
        catch (MembershipCreateUserException e)
        {
            ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
        }
    }
    return View(model);
}

然后,要获得额外的属性:

using (var context = new UsersContext())
                {
                    UserProfile profile = context.UserProfiles.First(x => x.UserName == User.Identity.Name);
                    // TODO: do something with the user profile
                }
于 2013-02-17T16:45:48.400 回答