- In this article we are going to see what to do when JavaScript is disabled on client browser and Remote attribute does not validate the logic.
- To understand this article better read Remote Validation article first, we have used same UserName validation scenario in this article.
What’s wrong when JavaScript is disabled?
When JavaScript is disabled in the browser, the <script> tags won't be interpreted and executed in your document, including all your jQuery and AJAX JS code.
Why JavaScript disabled?
It depends on user to user whether they want JavaScript enable on browser or not. Following are the most common reason to disable JavaScript on browser.
• Speed & Bandwidth
• Usability & Accessibility
• Platform Support
• Security
Why Server side logic?
The server side logic is necessary as client side logic does not work due to any reason it is always safe and good to have server side logic.
Controller:
[HttpPost]
public ActionResult Index(UserViewModel model)
{
RemoteValidationService service = new RemoteValidationService();
if (service.IsValidUserName(model.UserName))
{
ModelState.AddModelError("UserName", "Username already exist");
return View(model);
}
service.SaveUser(model);
return View();
}
The form is posted to the above Action method. In this method we are checking if username is present in database or not. If username is present in database then we are adding an error to ModelState making it invalid and returning the View with model having model error. So, this will show error message against the UserName control.Screenshot:
So it is good practice to have server side validation as backup to client side validation. This is how you can cover up the validation performed by Remote attribute by writing server side logic as well.
No comments:
Post a Comment