繁体   English   中英

在一个视图中具有某些公共属性的两种形式验证错误Asp.net mvc

[英]Two forms in one view with some common attributes Validation error Asp.net mvc

我认为有两种形式,但是两种形式的模型都是相同的。 在第一个表格中,我跳过了一个模型字段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>

您的基础模型可能相似,但这是创建视图模型的有用案例:特定于使用该视图的视图的数据模型的表示。 这样,您可以分别强制执行所需的状态,并在控制器中处理映射回数据模型的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM