I'm working on ASP.NET MVC4 with Razor, using a model-first Entity Framework connection. I have a Login form made by scratch, that requests for username, password, and Usertype. Unfortunately, the form is able to post even if the fields are blank, which leads to an exception being thrown.
Since I'm directly mapping towards my database through the model instead of using classes on the Models folder, i have no way of applying DataAnnotations. Is there another way to enforce form validation in order to avoid sending null values?
Since you can't use the
[Required]
attribute, you probably want to use client side validation. If you're usingHTML5
you can mark the fields asrequired
, you could make a custom helper to output something like the following:As demonstrated here (Without the helper method):
http://jsfiddle.net/ck4dL/
You could also validate the user input using
JavaScript
manually via.Or use a prebuilt library such as
jQuery validate
As an additional note it would be best to check these fields at the server level also. Your users may have JavaScript disabled or may deliberately send up malformed information.