Results 1 to 3 of 3

Thread: MVC - field validation

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019

    MVC - field validation

    I have a question: in the MVC pattern, should fields validation be done in View classes or Model classes? Why one and not the other?

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Manchester uk

    Re: MVC - field validation

    Generally you want to use Model validation, as the thing you trying to validate is your data which is held in your model.

    The main reason for having validation in your model is you can specify the model validation rules here and through model binding in your web pages you can return validation messages instantly to the user on the page if the model is invalid, and your not having to hit your ViewModel each time, if your model is invalid then the page wont submit or save.

    This nicely separates your validation from your actual actions.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: MVC - field validation

    There may be some validation that must be done server-side due to complexity of data requirements but, generally, you should aim to do the validation client-side via attributes decorating the properties of your model, e.g. a Required attribute on non-nullable data.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width