Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My recommendation is basically what you don't want: create new model classes, but use inheritance to avoid repeating the properties you want. If you are adamantly opposed to creating separate models, you might look into implementing <code>IValidatableObject</code> and have it inspect other properties before validating the properties that you wish to vary.</p> <p><strong>EDIT</strong>: I don't disagree with Tuliper's answer, but to flesh out my suggestions, consider a scenario in which you want to save a user's data. From one form, you are creating a user; from another, you are simply updating (this is a bit of a stretch but it's for purposes of illustration). The "create" form might require the name of a person referring the user, while the "update" form might not. </p> <p>Using inheritance, you could do the following:</p> <pre><code>public class SaveUserModel { public int? UserId { get; set; } ... } public class CreateUserModel : SaveUserModel { [Required] public string ReferredByName { get; set; } } </code></pre> <p>Using <code>IValidatableObject</code>, you could do it this way:</p> <pre><code>public class SaveUserModel : IValidatableObject { public int? UserId { get; set; } public string ReferredByName { get; set; } ... public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext) { // if UserId is null, we are creating a user vs. updating if (UserId != null &amp;&amp; string.IsNullOrWhiteSpace(ReferredBySiteUrl)) yield return new ValidationResult("Please specify the name of the person who referred you.", new[] { "ReferredByName" }); } } </code></pre> <p>To reiterate, I am not trying to push my answer. I would be inclined to reuse models if they are exactly the same across different views, but generally there are enough differences to warrant simply creating separate models. In the end, any perceived technical debt alleviated by adhering to DRY in this situation would a bit of a wash; models tend to be easy to maintain.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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