Validating forms in html
As several people have said, since you're using data annotation (the [Required] tag in your viewmodel), the good news is that you don't have to add too much to fix your problem.To prevent it from sending blank emails, add the following to your controller: The Model State will be valid only if the incoming data is not null, which will prevent the rest of the code from being reached (and sending the email).Joshua Studley is a Sr UI Engineer at Kelley Blue Book.He has over 15 years of front-end web development experience and has worked with leading interactive companies in a number of fields, from medical software to automotive media.There's a tutorial here on Model State that's worth reading.
Fugiat, vitae, distinctio, possimus repudiandae cupiditate ipsum excepturi dicta neque eaque voluptates tempora veniam esse earum sapiente optio deleniti consequuntur eos voluptatem.
You would add the validation check/message directly to your view, in the form group for the user input: The html helper Validation Message For will look at your model and grab the message you've listed as being for validation and will display it when the user attempts to post the page.
Along with the other data annotation on your model variables and a Model State check, it will prevent the user from going forward until the fields have content that meets your checks and prevent the rest of the controller method from executing in the event that something null gets past your checks.
The documentation can be found in the MSDN documentation and is built into the MVC system.
For greater control over what it does during the error process for errors inside the MVC system (not the Html errors), you can override the MVC method On Exception, but it will considerably complicate your coding process. My advice to you on that issue, however, would be to take advantage of the Elmah package instead of trying to write that yourself.