MVC模型验证不起作用

时间:2017-06-26 07:18:56

标签: c# asp.net-mvc asp.net-mvc-4

         <div class="form-group">
             <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                 @*<input type="text" name="pop_cust_name" id="pop_cust_name" class="form-control" placeholder="Full Name">*@
                 @*@Html.TextBox("Customer_Name", null, new { @class = "form-control", @placeholder = "Full Name" })*@
                 @Html.TextBoxFor(m => m.Customer_Name, new { @class = "form-control", @placeholder = "Full Name" })
                 @Html.HiddenFor( m => m.Customer_ID)
                 @Html.ValidationMessageFor(m => m.Customer_Name, null, new { @class = "text-danger" })
            </div>

            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_mob" id="pop_cust_mob" class="form-control" placeholder="Enter Mobile">*@
                @*@Html.TextBox("Customer_Mobile", null, new { @class = "form-control", @placeholder = "Enter Mobile" })*@
                @Html.TextBoxFor(m => m.Customer_Mobile, new { @class = "form-control", @placeholder = "Enter Mobile" })
                @Html.ValidationMessageFor(m => m.Customer_Mobile, null, new { @class = "text-danger" })
            </div>

            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="Email" name="pop_cust_email" id="pop_cust_email" class="form-control" placeholder="Enter Email">*@
                @*@Html.TextBox("Customer_Email", null, new { @class = "form-control", @placeholder = "Enter Email" })*@
                @Html.TextBoxFor(m => m.Customer_Email, new { @class = "form-control", @placeholder = "Enter Email" })
                @Html.ValidationMessageFor(m => m.Customer_Email, null, new { @class = "text-danger" })
            </div>
        </div>

        <div class="col col-sm-12 col-xs-12 col-md-12 col-lg-12" style="height:10px"></div>

        <div class="form-group">
            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_company" id="pop_cust_company" class="form-control" placeholder="Enter Company Name">*@
                @*@Html.TextBox("Customer_Company", null, new { @class = "form-control", @placeholder = "Enter Company Name" })*@
                @Html.TextBoxFor(m => m.Customer_Company, new { @class = "form-control", @placeholder = "Enter Company Name" })
            </div>

            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_fax" id="pop_cust_fax" class="form-control" placeholder="Enter Fax">*@
                @*@Html.TextBox("Customer_Fax", null, new { @class = "form-control", @placeholder = "Enter Fax" })*@
                @Html.TextBoxFor(m => m.Customer_Fax, new { @class = "form-control", @placeholder = "Enter Fax" })
            </div>

            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_other" id="pop_cust_other" class="form-control" placeholder="Enter Other">*@
                @*@Html.TextBox("Customer_Other", null, new { @class = "form-control", @placeholder = "Enter Other" })*@
                @Html.TextBoxFor(m => m.Customer_Other, new { @class = "form-control", @placeholder = "Enter Other" })
             </div>
          </div>

          <div class="col col-sm-12 col-xs-12 col-md-12 col-lg-12" style="height:10px"></div>

          <div class="col col-sm-12 col-xs-12 col-md-8 col-lg-8"></div>

          <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
            <div class="checkbox">
                <label>
                    <input type="checkbox">Same as billing address
                </label>
            </div>
        </div>

        <div class="col col-sm-12 col-xs-12 col-md-12 col-lg-12" style="height:10px"></div>

        <div class="form-group">
            <div class="col col-sm-12 col-xs-12 col-md-6 col-lg-6">
                @*<textarea class="form-control" name="pop_cust_bill_address" rows="3" placeholder="Billing address"></textarea>*@
                @*@Html.TextArea("Customer_BillingAddresss", null, new { @class = "form-control", @placeholder = "Billing address" })*@
                @Html.TextAreaFor(m => m.Customer_BillingAddresss, new { @class = "form-control", @placeholder = "Billing Address" })
                @Html.ValidationMessageFor(m => m.Customer_BillingAddresss, null, new { @class = "text-danger" })
            </div>
            <div class="col col-sm-12 col-xs-12 col-md-6 col-lg-6">

                @*<textarea class="form-control" name="pop_cust_ship_address" rows="3" placeholder="Shipping address"></textarea>*@
                @*@Html.TextArea("Customer_ShippingAddress", null, new { @class = "form-control", @placeholder = "Shipping address" })*@
                @Html.TextAreaFor(m => m.Customer_ShippingAddress, new { @class = "form-control", @placeholder = "Shipping Address" })
                @Html.ValidationMessageFor(m => m.Customer_ShippingAddress, null, new { @class = "text-danger" })
            </div>

        </div>

        <div class="col col-sm-12 col-xs-12 col-md-12 col-lg-12" style="height:10px"></div>

        <div class="form-group">
            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_pan_no" id="pop_cust_pan_no" class="form-control" placeholder="Enter PAN No">*@
                @*@Html.TextBox("Customer_PanNo", null, new { @class = "form-control", @placeholder = "Enter PAN No" })*@
                @Html.TextBoxFor(m => m.Customer_PanNo, new { @class = "form-control", @placeholder = "Enter PAN No" })
                @Html.ValidationMessageFor(m => m.Customer_PanNo, null, new { @class = "text-danger" })
            </div>
            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_gst_num" id="pop_cust_gst_num" class="form-control" placeholder="Enter GSTIN">*@
                @*@Html.TextBox("Customer_GSTIN", null, new { @class = "form-control", @placeholder = "Enter GSTIN" })*@
                @Html.TextBoxFor(m => m.Customer_GSTIN, new { @class = "form-control", @placeholder = "Enter GSTIN" })
            </div>
            <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4">
                @*<input type="text" name="pop_cust_gst_clr" id="pop_cust_gst_clr" class="form-control" placeholder="Enter CLR">*@
                @*@Html.TextBox("Customer_CLR", null, new { @class = "form-control", @placeholder = "Enter CLR" })*@
                @Html.TextBoxFor(M => M.Customer_CLR, new { @class = "form-control", @placeholder = "Enter CLR" })
            </div>
        </div>

        <div class="col col-sm-12 col-xs-12 col-md-12 col-lg-12" style="height:10px"></div>

            <div class="form-group">
                <div class="col col-sm-12 col-xs-12 col-md-8 col-lg-8">
                    @*<textarea class="form-control" rows="3" placeholder="Note"></textarea>*@
                    @*@Html.TextArea("Customer_Note", null, new { @class = "form-control", @placeholder = "Note" })*@
                    @Html.TextAreaFor(m => m.Customer_Note, new { @class = "form-control", @placeholder = "Note" })
                </div>

            </div>

        <div class="col col-sm-12 col-xs-12 col-md-4 col-lg-4"></div>
    </div>

    <div class="modal-footer" >
         <button type="submit" name="save_pop" id="save_pop" class="btn btn-success" formaction = '@Url.Action("AddCustomer", "Customers")'>Save</button> 
        <button type="submit" name="update_pop" id="update_pop" style="display:none" class="btn btn-success" formaction='@Url.Action("UpdateCustomer", "Customers")'>Update</button> 
        @*<input type="submit" value="Save" class="btn btn-success" onclick="location.href='@Url.Action("AddCustomer", "Customers")'" />*@

        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
    </div>
</form>

我的控制器代码是

[HttpPost]
public ActionResult AddCustomer(CustomerEntities customerentities)
{
    customerentities.Customer_Name = Request["Customer_Name"].ToString();
    customerentities.Customer_Mobile = Request["Customer_Mobile"].ToString();
    customerentities.Customer_Email = Request["Customer_Email"].ToString();
    customerentities.Customer_Company = Request["Customer_Company"].ToString();
    customerentities.Customer_Fax = Request["Customer_Fax"].ToString();
    customerentities.Customer_Other = Request["Customer_Other"].ToString();
    customerentities.Customer_BillingAddresss = Request["Customer_BillingAddresss"].ToString();
    customerentities.Customer_ShippingAddress = Request["Customer_ShippingAddress"].ToString();
    customerentities.Customer_PanNo = Request["Customer_PanNo"].ToString();
    customerentities.Customer_GSTIN = Request["Customer_GSTIN"].ToString();
    customerentities.Customer_CLR = Request["Customer_CLR"].ToString();
    customerentities.Customer_Note = Request["Customer_Note"].ToString();
    customerentities.User_ID = 1;

    if (ModelState.IsValid)
    {
        CustomerRepository custRepo = new InvoiceProcessingSystem.CustomerRepository();

        if (custRepo.AddCustomer(customerentities))
        {
            TempData["AddCustomer"] = "Customer details added successfully";
        }

        return RedirectToAction("Index");
    }

    return RedirectToAction("Index", customerentities);
}

由于没有控制器AddCustomer的视图所以我将它与模型一起重定向到Index视图,因为我试图在单个视图中进行CRUD。

最后我的模型代码是:

public int Customer_ID { get; set; }

[Required(ErrorMessage = "Customer Name is required")]
public string Customer_Name { get; set; }

//[Required(ErrorMessage = "Customer Mobile is required")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Mobile number")]        
public string Customer_Mobile { get; set; }

//[Required(ErrorMessage = "Customer Email is required")]
[EmailAddress(ErrorMessage = "Enter a valid Email Address")]
public string Customer_Email { get; set; }

//[Required(ErrorMessage = "Customer Company is required")]
public string Customer_Company { get; set; }

public string Customer_Fax { get; set; }

public string Customer_Other { get; set; }

//[Required(ErrorMessage = "Customer Billing is required")]
[MaxLength(500)]
public string Customer_BillingAddresss { get; set; }

//[Required(ErrorMessage = "Customer Shipping Address is required")]
[MaxLength(500)]
public string Customer_ShippingAddress { get; set; }

//[Required(ErrorMessage = "Customer PAN No. is required")]
[RegularExpression(@"[A-Z]{5}\d{4}[A-Z]{1}", ErrorMessage = "Not a valid Mobile number")]
public string Customer_PanNo { get; set; }

//[Required(ErrorMessage = "Customer GSTIN is required")]
public string Customer_GSTIN { get; set; }

//[Required(ErrorMessage = "Customer CLR is required")]
public string Customer_CLR { get; set; }

public string Customer_Note { get; set; }

//[Required(ErrorMessage = "Customer Name is required")]
public int User_ID { get; set; }

我不太确定我在这里做错了什么,因为我是MVC的新手,但我之前没有使用模型验证,但我无法弄清楚我的代码在这里是什么问题。

1 个答案:

答案 0 :(得分:0)

如果您希望客户端验证,请确保以下内容:

第1步:在web.config中添加以下内容

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

第2步:参考jquery.validate.js

中的index.cshtml
相关问题