Reputation: 582
This is a part of my view
@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 required validation to it. The billing_address textbox is not a part of the models.employee. I am using mvc5 So how to add the validator?
Upvotes: 6
Views: 22185
Reputation: 17182
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()
is used for printing validation message.validate
and unobtrusive
JavaScript files.Upvotes: 11
Reputation: 1283
Don't know if it's what you're looking for, but look a this :
In your BuynowForm
method, add a string
parameter called bill_address
.
On the form submit, you'll get the input value in this string
, and you'll be able to make your validations.
Hope this helps
Upvotes: 0