我已经为我的MVC Web应用程序创建了一个注册页面。当我尝试注册用户时,来自cuisine_1,cuisine_2和cuisine_3选择框的信息不会输入到UserCuisineInfo表中。但是,会创建一个带有id的新行,并在网页上显示错误消息,指出要求cooking_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; }
}
答案 0 :(得分:2)
你给所有的下拉菜单都称为烹饪。您需要将它们更改为Cuisine_1,Cuisine_2和Cuisine_3。
答案 1 :(得分:1)
您有三个名称相同的下拉列表Cuisine
。更新它们以便它们反映模型的属性:
@Html.DropDownList("Cuisine_1", ...
@Html.DropDownList("Cuisine_2", ...
@Html.DropDownList("Cuisine_3", ...
当渲染这些控件时,它们将生成一个表单控件,如:
<select name="Cuisine_1" ....
name
属性的值用于模型绑定,并且必须与ViewModel中属性的名称相对应。