Not only does MVC support server-side validation, client-side validation is a possibility too. Below is a sample form that sets up validation using client-side javascript. These client-side features are made available from the MicrosoftMvcValidation.js file. Take a look at the sample form with validation below.
<% Html.EnableClientValidation(); %>
<% Html.BeginForm(); %>
<%= Html.ValidationSummary("The following errors have happened:") %>
<fieldset>
<legend>Form Values</legend>
<div>
First: <%= Html.TextBoxFor(i => i.FirstName) %>
<%= Html.ValidationMessageFor(i => i.FirstName, "*") %>
</div>
<div>
Last: <%= Html.TextBoxFor(i => i.LastName) %>
<%= Html.ValidationMessageFor(i => i.LastName, "*")%>
</div>
<div>
Email: <%= Html.TextBoxFor(i => i.Email) %>
<%= Html.ValidationMessageFor(i => i.Email, "*")%>
</div>
<input type="submit" value="Save" />
</fieldset>
<% Html.EndForm(); %>
Client-side validation begins with a method call to enable client-side validation. Under the scenes, this enables some of the built-in features, plus ensures some of the requirements within the UI happen (for instance, it ensures that each form has a unique ID). If you compare this example with my server-side validation blog entry, you will see the form is the same; all that's different is the call to this new client-side method. Adding this method adds some new client-side code that sets up the components to do the validation. This code looks like the following:
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"FirstName","ReplaceValidationMessageContents":false,"ValidationMessageId":"FirstName_validationMessage","ValidationRules":[{"ErrorMessage":"First name required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"LastName","ReplaceValidationMessageContents":false,"ValidationMessageId":"LastName_validationMessage","ValidationRules":[{"ErrorMessage":"Last name required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Email","ReplaceValidationMessageContents":false,"ValidationMessageId":"Email_validationMessage","ValidationRules":[{"ErrorMessage":"Email required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":true,"ValidationSummaryId":"validationSummary"});
The client-side components contain the array of validators to apply to each form element. It maps the property of the model object(which is equivalent to the ID of the control), mapping these to the validation rules to validate against. For instance, you see a lot of required validators, which refers to a client-side implementation of each of the server-side component. It also contains the error message to display to the user when the validation fails.
The action method to get and post the data to the view looks like the same as our server-side validation example too. Check the example below:
[HttpGet]
public ActionResult AjaxIndex()
{
return View(new ValidationTestClass { FirstName = "Bob", LastName = "Anderson", Email = "boba@test.com" });
}
[HttpPost]
public ActionResult AjaxIndex(ValidationTestClass val)
{
if (!ModelState.IsValid)
return View();
return RedirectToAction("Index", "Home");
}
First, the view that posts back fires the client-side validation first. If it detects an error, the field is flagged with a message generated on the client-side. If successful, the form posts back and processes the equivalent server-side validator, and if that check fires, the ModelState.IsValid property will evaluate to true.
What is so SPECIAL on ASPHostDirectory.com .NET MVC Hosting?
We know that finding a cheap, reliable web host is not a simple task so we've put all the information you need in one place to help you make your decision. At ASPHostDirectory, we pride ourselves in our commitment to our customers and want to make sure they have all the details they need before making that big decision.
We will work tirelessly to provide a refreshing and friendly level of customer service. We believe in creativity, innovation, and a competitive spirit in all that we do. We are sound, honest company who feels that business is more than just the bottom line. We consider every business opportunity a chance to engage and interact with our customers and our community. Neither our clients nor our employees are a commodity. They are part of our family.
The followings are the top 10 reasons you should trust your online business and hosting needs to us:
- FREE domain for Life -ASPHostDirectory gives you your own free domain name for life with our Professional Hosting Plan and 3 free domains with any of Reseller Hosting Plan! There's no need to panic about renewing your domain as ASPHostDirectory will automatically do this for you to ensure you never lose the all important identity of your site
- 99,9% Uptime Guarantee - ASPHostDirectory promises it's customers 99.9% network uptime! We are so concerned about uptime that we set up our own company to monitor people's uptime for them called ASPHostDirectory Uptime
- 24/7-based Support - We never fall asleep and we run a service that is opening 24/7 a year. Even everyone is on holiday during Easter or Christmast/New Year, we are always behind our desk serving our customers
- Customer Tailored Support - if you compare our hosting plans to others you will see that we are offering a much better deal in every aspect; performance, disk quotas, bandwidth allocation, databases, security, control panel features, e-mail services, real-time stats, and service
- Money Back Guarantee - ASPHostDirectory offers a ‘no questions asked' money back guarantee with all our plans for any cancellations made within the first 30 days of ordering. Our cancellation policy is very simple - if you cancel your account within 30 days of first signing up we will provide you with a full refund
- Experts in .Net MVC Hosting - Given the scale of our environment, we have recruited and developed some of the best talent in the hosting technology that you are using. Our team is strong because of the experience and talents of the individuals who make up ASPHostDirectory
- Daily Backup Service - We realise that your website is very important to your business and hence, we never ever forget to create a daily backup. Your database and website are backup every night into a permanent remote tape drive to ensure that they are always safe and secure. The backup is always ready and available anytime you need it
- Easy Site Administration - With our powerful control panel, you can always administer most of your site features easily without even needing to contact for our Support Team. Additionally, you can also install more than 100 FREE applications directly via our Control Panel in 1 minute!
Happy Hosting!
0 comments:
Post a Comment