Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于在数据库[ASP.NET MVC 4]中保存空值的RadioButton_C#_Asp.net Mvc_Asp.net Mvc 4_Radio Button_Html.dropdownlistfor - Fatal编程技术网

C# 用于在数据库[ASP.NET MVC 4]中保存空值的RadioButton

C# 用于在数据库[ASP.NET MVC 4]中保存空值的RadioButton,c#,asp.net-mvc,asp.net-mvc-4,radio-button,html.dropdownlistfor,C#,Asp.net Mvc,Asp.net Mvc 4,Radio Button,Html.dropdownlistfor,我正在使用ASP.NETMVC4模板创建我的应用程序 我在UserProfile字段中创建了一个UserType作为字符串,并进行了迁移以更新数据库。没错 我的问题是,当我在register视图中注册一个新用户时,应用程序会保存所有字段,但是UserType会保存null,我如何解决它?我读了一些有类似问题的主题,但我不知道如何对我起作用。我的代码如下: 在@StephenMuecke的帮助下,我在这里发布更新的代码,问题尚未解决: //Models [Table("UserProfile")]

我正在使用ASP.NETMVC4模板创建我的应用程序

我在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>
//模型
[表(“用户档案”)]
公共类用户配置文件
{
[关键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串用户类型{get;set;}//flag de tipo usuário musico ou ouvinte
}
公共类注册器模型
{
[必需]
[显示(Name=“User Name”)]
公共字符串用户名{get;set;}
[必需]
[StringLength(100,ErrorMessage={0}的长度必须至少为{2}个字符。”,MinimumLength=6)]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[数据类型(数据类型.密码)]
[显示(Name=“确认密码”)]
[比较(“密码”,ErrorMessage=“密码和确认密码不匹配。”)]
公共字符串ConfirmPassword{get;set;}
[必需]
[显示(Name=“Tipo de Usuario”)]
公共字符串用户类型{get;set;}
}
//控制器
//获取:/Account/注册
[异名]
公众行动结果登记册()
{
返回视图();
}
//
//职位:/Account/Register
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共操作结果寄存器(RegisterModel模型)
{
if(ModelState.IsValid)
{
//尝试注册用户
尝试
{
var user=new UserProfile(){UserName=model.UserName,UserType=model.UserType};
WebSecurity.CreateUserAndAccount(model.UserName、model.Password);
WebSecurity.Login(model.UserName、model.Password);
返回重定向到操作(“索引”、“主页”);
}
捕获(MembershipCreateUserException e)
{
AddModelError(“,ErrorCodeToString(例如StatusCode));
}
}
//看法
@模型测试TCC2.Models.RegisterModel
乌苏瓦里奥酒店
@*DropDownListFor(m=>m.UserType,userTypes)*@
@RadioButton(m=>m.UserType,“Ouvinte”,新的{id=“Ouvinte”})
@Html.Label(“Ouvinte”)
@RadioButton(m=>m.UserType,“Musico”,新的{id=“Musico”})
@Html.Label(“Musico”)
编辑:

为了解决radiobutton的问题,我需要更新调用UserProfile类的PostRegister方法

代码如下:

现在我要创建一个新主题,询问在注册过程中遇到的异常错误 主题链接:

试试看

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

UserType
“Ouvinte”和“Musico”的可能值?值在radiobutton中定义,即:Ouvinte和Musico。@StephenMueckeIt不起作用…也许,你知道我是否必须对我的控制器类进行一些更新吗?刚刚在我的项目中进行了测试,效果很好。你发布的方法签名是什么?这是我在AccountController类中的POST和get方法。恐怕radiobutton不起作用工作正常,这是可能发生的?您的模型是
UserProfile
,但您发回
public ActionResult Register(RegisterModel模型)
?视图中的模型应该是
RegisterModel
,或者方法签名应该是
public ActionResult Register(UserProfile模型)
对不起,我耽搁了。在我的视图中,
Register
,我正在使用model
RegisterModel
,如果我在该视图中更改为
UserProfile
,则
password
字段显示错误,因为在UserProfile中,没有字段密码,明白吗?这是我的RegisterModel