RadioButton用于在DB中保存Null〔ASP.NET MVC 4〕

本文关键字:NET ASP MVC Null 用于 DB 保存 RadioButton | 更新日期: 2023-09-27 18:26:04

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

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

我的问题是,当我在register视图中注册新用户时,应用程序会保存所有字段,但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>

编辑:

为了解决单选按钮的问题,我需要更新调用UserProfile类的Post Register方法。

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

现在我要创建一个新的主题来询问我在注册过程中遇到的异常错误主题链接:在ASP.NET 上用户注册期间的MemberShipException

RadioButton用于在DB中保存Null〔ASP.NET MVC 4〕

尝试

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