Server Side Required Field Validator Sample in MVC: Day 27

In this article we will see how to apply a required field validator from the server side in MVC.

  1. Create a MVC project from the "Empty" template.

  2. Right-click on "Models", select "Add" >>"Class…".

    add new class

  3. Name the class as "Employee" and click on the "Add" button.

    debugger visualize

  4. In the employee class create employee properties like EmployeeId, FirstName, LastName and so on.

    employee cs file

  5. Now add a controller. Right-click on "Controllers", select "Add" >> "Controller…".

    add controller

  6. Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.

    MVC5 Controller

  7. Name the controller as in the following:

    employee controller

  8. Now add a view. Right-click on the "Index" action method and select "Add View…".

    employee controller cs file

  9. Name the view "Index". Select "Empty (without model)" from the template. Click on the "Add" button.

    index view

  10. Now add two action result methods to the employee controller with the same name, one is for the HttpGet and the other is for the HttpPost. In the HttpPost "Create" method pass an Employee class object as parameter. When we press the submit button it will call this HttpPost method.

    function

  11. To add a view for the "Create" method, right-click on the "Create" action result method and select "Add View…".

    add view

  12. Name the view "Create". Since we will show the create form of employee, select "Create from template". From the model class drop down, select "Employee" as the model. Click on the "Add" button.

    create view

  13. It will automatically create the view from the model.

    create cs html

  14. Add the "[Required]" attribute to the field that you want to make mandatory for insertion. The Required attribute requires the "System.ComponentModel.DataAnnotations" namespace.

    require field validater

  15. Now add two action result methods to the employee controller with the same name, one is for the HttpGet and the other one is for the HttpPost. In the HttpPost "Create" method pass an Employee class object as parameter. When we press the submit button it will call this HttpPost method.

    create function

  16. Run the project and call the "Create" action method of the Employee controller from the browser.

    employee

  17. Now do not fill in the form and press the "Create" button and you can see the error message against the fields where we set the "Required" attribute in the model.

    require field

Change Error Message

  1. In the model set the validation attribute "ErrorMessage", that sets an error message to associate with a validation control when validation fails. Here in our example we change it to "First Name cannot be blank."

  2. Run the project and call the "Create" action method of the Employee controller from the browser.

  3. Now do not fill in the form and press the "Create" button and you can see the changed error message.

    require field validater model

Read Error Message from Resource File

  1. Right-click on the project, select "Add" >> "New Item…".

    add new item

  2. Select "Resources File", provide an appropriate name and press the "Add" button.

    resource file

  3. Set Resource Name and Value. Here we set "Value Can Not Be Blank" for "RequiredFieldMessage".

    require field message

  4. Now in the model, set the validation attribute, in other words ErrorMessageResourceName and ErrorMessageResourceType as in the following:

    employee class

  5. Run the project and call the "Create" action method of the Employee controller from the browser.

  6. Now do not fill in the form and press the "Create" button and you can see the message that we set in the resource file.

    value can not be blank

<< Client Side Required Field Validator Sample in MVC: Day 26


Similar Articles