Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere do you put your validation in asp.net mvc 3?
    primarykey
    data
    text
    <p>One common recommended practice in asp.net mvc is that <a href="http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/" rel="nofollow noreferrer">you should not send your business models to your views</a>.. instead you should create viewmodels specific to each view.</p> <p>When that is done and you call the ModelState.IsValid method in your controller you are effectively checking the validity of the viewmodel but not the business object.</p> <p>What is the conventional approach to dealing with this?</p> <pre><code>public class Person { public int ID {get; set;}; [Required] public string Name {get; set;} [Required] public string LastName {get; set;} public virtual ICollection&lt;Exam&gt; Exams {get; set;} } public class PersonFormViewModel { public int ID {get; set;}; [Required] public string Name {get; set;} [Required] public string LastName {get; set;} } </code></pre> <p>This is exactly what I have right now but Im not sure if the [Required] attribute should appear on both models or just the ViewModel or just the Business Model.</p> <p>Any tips on this issue are appreciatedd.</p> <p>More links to support my claim that it is a common good practice to always use view models.</p> <p><a href="https://stackoverflow.com/questions/5128303/how-to-add-validation-to-my-pocotemplate-classes">How to add validation to my POCO(template) classes</a></p> <p><a href="http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload