选择不发布信息MVC身份



我已经为我的MVC Web应用程序创建了一个注册页面。当我尝试向用户注册Cuisine_1的信息时,Cuisine_2和Cuisine_3选择框未输入USERCUISINEINFO表中。但是,使用ID创建了一个新的行,并且在网页上显示了一个错误消息,说Cuisine_1,Cuisine_2和Cuisine_3字段需要。

注册网页

@model Bitev2.Models.RegisterViewModel
@{
    ViewBag.Title = "Register";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary()
    <div class="form-group">
        @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
    @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
    </div>
</div>
    <div class="form-group">
        @Html.LabelFor(m => m.Gender, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownList("Gender", new SelectList(new[] {"Female", "Male"}))
        </div>
    </div>
    <div class="form-group">
    @Html.LabelFor(m => m.DateofBirth, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.DateofBirth, new { @class = "form-control", @InputType = "date" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.EmailID, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.EmailID, new { @class = "form-control" })
    </div>
</div>
    <div class="form-group">
            @Html.LabelFor(m => m.Cuisine_1, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.DropDownList("Cuisine", new SelectList(new[] { "African", "American",
               "Asian", "Bakery", "British", "Carribean", "Chinese", "Continetal", "Eastern European", 
               "French", "German", "Greek", "Indian", "Irish", "Italian", "Japanese", "Mediteranean", 
               "Mexican", "Middle Eastern", "Pizza", "Pub", "Steakhouse", "Sushi", "Thai", "Vegetarian", 
               "Vietnamese"  }))
            </div>
        </div>
    <div class="form-group">
            @Html.LabelFor(m => m.Cuisine_2, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.DropDownList("Cuisine", new SelectList(new[] { "African", "American", 
               "Asian", "Bakery", "British", "Carribean", "Chinese", "Continetal", "Eastern European", 
               "French", "German", "Greek", "Indian", "Irish", "Italian", "Japanese", "Mediteranean",
               "Mexican", "Middle Eastern", "Pizza", "Pub", "Steakhouse", "Sushi", "Thai", "Vegetarian", 
               "Vietnamese"  }))
            </div>
        </div>
    <div class="form-group">
            @Html.LabelFor(m => m.Cuisine_3, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.DropDownList("Cuisine", new SelectList(new[] { "African", "American", 
               "Asian", "Bakery", "British", "Carribean", "Chinese", "Continetal", "Eastern European", 
               "French", "German", "Greek", "Indian", "Irish", "Italian", "Japanese", "Mediteranean",
               "Mexican", "Middle Eastern", "Pizza", "Pub", "Steakhouse", "Sushi", "Thai", "Vegetarian", 
               "Vietnamese"  }))
            </div>
        </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

注册查看模型

public class RegisterViewModel
    {
        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string EmailID { get; set; }
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
        [Required]
        [Display(Name = "Cuisine")]
        public string Cuisine { get; set; }
        [Required]
        [Display(Name = "Gender")]
        public string Gender { get; set; }
        [Required]
        [DataType(DataType.Date)]
        [Display(Name = "Date of Birth")]
        public string DateofBirth { 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 = "Cuisine 1")]
        public string Cuisine_1 { get; set; }
        [Required]
        [Display(Name = "Cuisine 2")]
        public string Cuisine_2 { get; set; }
        [Required]
        [Display(Name = "Cuisine 3")]
        public string Cuisine_3 { get; set; }
    }

帐户控制器寄存器查看

// POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, EmailID = model.EmailID, Gender = model.Gender, DateofBirth = model.DateofBirth,
                UserCuisineInfo = new UserCuisineInfo
                {
                    Cuisine_1 = model.Cuisine_1,
                    Cuisine_2 = model.Cuisine_2,
                    Cuisine_3 = model.Cuisine_3
                }
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Profile");
                }
                else
                {
                    AddErrors(result);
                }
            }

应用程序用户模型

public class ApplicationUser : IdentityUser
    {
        // stores users details in the user table
        public string FirstName { get; set; }
        public string LastName {get; set;}
        public string EmailID {get; set;}
        public string Image { get; set; }
        public string Gender { get; set; }
        public string DateofBirth { get; set; }
        // stores users cuisine choice in a separate table
        public virtual UserCuisineInfo UserCuisineInfo { get; set; }

    }

usercuisineinfo

public class UserCuisineInfo
    {
        public int Id { get; set; }
        public string Cuisine_1 { get; set; }
        public string Cuisine_2 { get; set; }
        public string Cuisine_3 { get; set; }
    }

您给所有下拉菜单的名字命名为美食。您需要将它们更改为cuisine_1,cuisine_2和cuisine_3。

您有三个称为 Cuisine的同样命名的下拉列表。更新它们,以便它们反映您的模型上的属性:

 @Html.DropDownList("Cuisine_1", ...
 @Html.DropDownList("Cuisine_2", ...
 @Html.DropDownList("Cuisine_3", ...

当渲染这些控件时,它们将产生一个形式控件,例如:

<select name="Cuisine_1" ....

name属性的值在模型绑定中使用,并且必须对应于viewModel中的属性名称。

相关内容

  • 没有找到相关文章

最新更新