How to add validators for @ Html.TextBox () without model

This is part of my performance

@model bhavin.Models.Employee

@using (Html.BeginForm("BuynowForm", "Home"))
{
<div class="form-group">
   <label>Billing Address</label>
   @Html.TextBox("bill_address", null, new { @class = "form-control valid" })
</div>
<p>
<input type="submit" value="Submit" class="btn btn-primary" />
</p>
 }

      

I want to add the required validation to it. The billing_address text field is not part of the model.employee. I am using mvc5 So how do I add a validator?

+3


source to share


2 answers


Add data-val

and data-val-required

attribute for Html.TextBox()

as shown below.

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>    

@using (Html.BeginForm("",""))
{
    @Html.ValidationSummary()
    @Html.TextBox("bill_address", null, new { @class = "form-control valid", @data_val = "true", @data_val_required = "Billing Address is required" })
    <input type="submit" value="Click" id="btnSubmit" />
}

      



Note

  • @Html.ValidationSummary()

    used to print a verification message.
  • Don't forget to include - validate

    and unobtrusive

    JavaScript files.
+7


source


Don't know if this is what you are looking for, but look at this:

In your method, BuynowForm

add a parameter string

called bill_address

.



On the submit form, you will receive an input value in this string

and you can do your validations.

Hope it helps

0


source







All Articles