我认为有两种形式,但是两种形式的模型都相同。在第一种形式中,我跳过了一个模型字段ShopAddress
,因此是[Required]字段,但是该字段在第一种形式中没有必要,因此当我提交表单时,我遇到了验证错误。在第二种形式中,我不希望UserName
字段也是[required]字段。所以在提交表单上也得到验证错误的任何解决方案?希望您能理解我的问题谢谢:)
模型
public partial class User
{
[Required(ErrorMessage ="The Name field is required")]
[Display(Name = "Your Name")]
[MaxLength(50 , ErrorMessage = "Name Cannot Be Longer Than 50 Characters")]
[RegularExpression(@"^[a-zA-Z \s]+$", ErrorMessage = "Input Allows Only Alphabets")]
public string FullName { get; set; }
[Required]
[Display(Name = "User Name")]
[MinLength(4, ErrorMessage = "Input Allows Minimum 4 Characters")]
[MaxLength(10, ErrorMessage = "User Name Cannot Be Longer Than 10 Characters")]
[RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "User Name Does Not Contain Space and Special Characters.")]
public string UserName { get; set; }
[Required]
[Display(Name = "Email")]
[MaxLength(50, ErrorMessage = "Email Cannot Be Longer Than 50 Characters")]
[RegularExpression(@"^[a-zA-Z-._0-9]+@[a-zA-Z]+\.[A-Za-z]+$", ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
[Required]
public string ShopAddress { get; set; }
[Required]
[Display(Name = "Phone")]
[MaxLength(11, ErrorMessage = "Please Enter a Valid Phone Number")]
[MinLength(11, ErrorMessage = "Please Enter a Valid Phone Number ")]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Input Allows Only Numbers")]
public string Phone { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
[MaxLength(30, ErrorMessage = "Password Cannot Be Longer Than 30 Characters")]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Confirm Password Does'nt Match With Password")]
public string ConfirmPassword { get; set; }
}
控制器
[HttpPost]
public ActionResult Register(User user)
{
if (ModelState.IsValid)
{
//add in db
}
return view()
}
查看
@model RentalServices.Models.User
<div class="hideSingleUserForm">
<div class="row">
<div class="col-sm-11">
@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Your Name" } })
@Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter User Name" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Your Valid Phone Number" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Your Valid Email Address" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Your Password" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control", placeholder = "Confirm Password" } })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="text-center">
<div class="">
<button type="submit" class="roundSubmitBtn margin-top-20">CREATE ACCOUNT</button>
</div>
</div>
}
</div>
</div>
</div>
<div class="hideBusinessForm" style="display:none">
<div class="row">
<div class="col-sm-11">
**Second Form**
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @Id = "ShopForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
<label>Shop Name</label>
@Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control", id = "1", placeholder = "Enter Your Name" } })
@Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
<label>Shop Address</label>
@Html.EditorFor(model => model.ShopAddress, new { htmlAttributes = new { @class = "form-control", id = "2", placeholder = "Enter User Name" } })
@Html.ValidationMessageFor(model => model.ShopAddress, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control", id = "3", placeholder = "Enter Your Valid Phone Number" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", id = "4", placeholder = "Enter Your Valid Email Address" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", id = "5", placeholder = "Enter Your Password" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 col-md-12 col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "" })
@Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control", id = "6", placeholder = "Confirm Password" } })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="text-center">
<div class="">
<button type="submit" class="roundSubmitBtn margin-top-20" id="SubmitShopForm">CREATE ACCOUNT</button>
</div>
</div>
}
</div>
</div>
</div>
答案 0 :(得分:-1)
您的基础模型可能相似,但这是创建视图模型的有用情况:特定于使用该视图的视图的数据模型的表示。这样,您可以分别强制执行所需的状态,并在控制器中处理映射回数据模型的操作。