RadioButtonFor在DB中保存Null [ASP.NET MVC 4]

时间:2014-10-09 23:50:42

标签: c# asp.net-mvc asp.net-mvc-4 radio-button html.dropdownlistfor

我正在使用ASP.NET mvc 4模板来创建我的应用程序。

我在UserProfile字段中创建了一个UserType字符串,并进行迁移以更新数据库。没错。

我的问题是,当我在注册视图中注册一个新用户时,应用程序保存所有字段,但是UserType,它保存为null,我该如何解决?我读了一些类似问题的主题,但我不知道怎么会对我有用。我的代码如下:

在@StephenMuecke的帮助下,我在这里发布了更新的代码,问题尚未解决:

//Models
[Table("UserProfile")]
        public class UserProfile
        {
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int UserId { get; set; }
            public string UserName { get; set; }
            public string UserType { get; set; } //flag de tipo usuário musico ou ouvinte


        }

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; }

        [Required]
        [Display(Name = "Tipo de Usuario")]
        public string UserType { get; set; }


    }

//Controller
        // GET: /Account/Register

        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    var user = new UserProfile() { UserName = model.UserName, UserType = model.UserType };

                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");

                }
                catch (MembershipCreateUserException e)

                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

//View
@model TestTcc2.Models.RegisterModel
 <div class="form-group">
                        <label for="icode" class="col-md-3 control-label">Tipo de usuário</label>
                        <div class="col-md-9">
                            @*Html.DropDownListFor(m=> m.UserType, userTypes)*@
                            @Html.RadioButtonFor(m => m.UserType, "Ouvinte", new { id = "Ouvinte" })
                            @Html.Label("Ouvinte")
                            @Html.RadioButtonFor(m=>m.UserType, "Musico", new { id= "Musico" })
                            @Html.Label("Musico")
                        </div>
                    </div>

编辑:

要解决radiobutton的问题,我需要更新调用UserProfile类的Post Register方法。

这是代码:http://pastebin.com/Mwd4QD6U

现在我正在创建一个新主题来询问我在注册期间遇到的异常错误 主题链接:MemberShipException during the User Register at ASP.NET

1 个答案:

答案 0 :(得分:1)

尝试

@Html.RadioButtonFor(m => m.UserType, "Ouvinte", new { id = "Ouvinte" })
@Html.Label("Ouvinte")
@Html.RadioButtonFor(m=>m.UserType, "Musico", new { id= "Musico" })
@Html.Label("Musico")