Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:24:34+00:00 2026-06-09T11:24:34+00:00

I have the following working system and looking for ways to make it DRY:

  • 0

I have the following working system and looking for ways to make it DRY:

public class EMailMetaData
{
  [Display(Prompt="myemail@mydomain.com"])
  public string Data;
}
public class PhoneMetaData
{
  [Display(Prompt="+1 (123) 456-7890"])
  public string Data;
}
public class AddressMetaData
{
  [Display(Prompt="Central st. W., St Francisco, USA"])
  public string Data;
}
// 7 more metadata templates

public class ContactVM
{
  [Required]
  public string DataLabel { get; set; }

  [Required(ErrorMessage="Please fill in the data field")]
  public string Data { get; set; }
}

[MetadataType(typeof(EmailMetaData))]
EmailVM : ContactVM
{
}
[MetadataType(typeof(PhoneMetaData))]
PhoneVM : ContactVM
{
}
[MetadataType(typeof(AddressMetaData))]
AddressVM : ContactVM
{
}
// 7 more contact view models

Controller is obviously initializes them with right content and in the view I run over the foreach loop of ContactVMs having TemplateEditor for every one of contacts: EmailVM.cshtml, PhoneVM.cshtml, AddressVM.cshtml UrlVM.cshtml etc.

The main view looks (omitting all the setup and details like this:

@model ContactsVM
foreach (var contact in Model.Contacts)
{
  @Html.EditorFor(m => contact)
}

and under EditorTemplates

@model EmailVM
@Html.EditorFor(model => model.DataLabel)
@Html.EditorFor(model => model.Data)
<br />
@Html.ValidationMessageFor(model => model.DataLabel)
@Html.ValidationMessageFor(model => model.Data)

… and obviously few more editor templates for every view model I have defined.

So in simple words – very similar contact types with minor differences in watermarking, naming, validation, but essentially all strings and all have the same fields (address is one long string rather than struct, same for all of them).

My question is not specific for watermarking, it can be any of properties – name, description, prompt etc.

[Display(Name="name", Description="description", Prompt="prompt")]

Its all pretty much working and showing right labels, watermarks for each, but it seems like huge DRY violation since all the template editors are exactly the same except the model type. What I show is here simplification to concentrate on the problem at hand, main view and editor templates are significantly more complicated that what you see here, so duplication is tremendous.

Can any of you suggest better way of making it not duplicating so much code please?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:24:37+00:00Added an answer on June 9, 2026 at 11:24 am

    To answer my own question:

    It is blunt solution but much simpler than those suggested and most important requires least duplication compared to straightforward solution (although not too elegant considering we’re in object oriented world):

    1. Define one view with base class (or interface, however you define it) as your model.
    2. In the view cast the model variable to the right class
    3. In the view, use the cast variable rather than the model

    So Contact.cshtml will look like this:

    @model ContactVM
    @* Do tons of stuff that is the same between views (not depending on data annotation) *@
    @Html.Partial("_ContactDataAnnotation", Model)
    @* Continue doing lots of stuff that is the same between all those classes
    

    Calling Contact.cshtml editor template will go in the following manner (thanks Serg):

    @foreach (var c in Model.Contacts)
    {
      @Html.EditorFor(m => c, "Contact")
    }
    

    The partial view that is dedicated to only show the right data annotations _ContactDataAnnotation.cshtml will look like this:

    @using <My Model Namespace>
    @model ContactVM
    @switch (Model.GetType().Name)
    {
      case "EmailVM":
        EmailVM e = Model as EmailVM;
        @Html.EditorFor(model => e.DataLabel)
        @Html.EditorFor(model => e.Data)
        <br />
        @Html.ValidationMessageFor(model => e.DataLabel)
        @Html.ValidationMessageFor(model => e.Data)
        break;
      case "PhoneVM":
        PhoneVM p = Model as PhoneVM;
        @Html.EditorFor(model => p.DataLabel)
        @Html.EditorFor(model => p.Data)
        <br />
        @Html.ValidationMessageFor(model => p.DataLabel)
        @Html.ValidationMessageFor(model => p.Data)
        break;
      // same thing 7 more times for all children of ContactVM, since MVC does not applying polymorphism to data annotations UNFORTUNATELY
      default:
        @Html.EditorFor(model => model.DataLabel)
        @Html.EditorFor(model => model.Data)
        <br />
        @Html.ValidationMessageFor(model => model.DataLabel)
        @Html.ValidationMessageFor(model => model.Data)
      }
    

    By this the duplication will only exist in places that is required, rather than having the same editor template duplicated 10 times with different names and model classes.

    I know it contradicts the title of the question “best practices”, but unfortunately this is the easiest and most minimalistic way to go. Other solutions, as Serg pointed out, are way too complex and require deep MVC infrastructure intervention, which I don’t like spending time on, as well as having prompts and tooltips injected rather than defined in data annotation, which appears to be a standard way of decorating and validating your models.

    I consider my solution to be a workaround MVC limitation of lacking polymorphism for data annotations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In YUI I have following code working for mouse wheel. How do I make
I have the following working tar -pcvf base.tar input/myPacket/my2 --exclude-vcs input/myPacket/my3/*.bmp When i have
We have the following code working for a complex rails form with checkboxes. I'm
Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);
I have following code And it is working fine in AIR device simulator on
I am working with NSCache in iOS i have following code in .h file
On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;
I have the following code which is working, I was wondering if this can
I have the following: An environment that is working like a hash for rows
I have the following PHP script with the working (according to JsonViewer ) JSON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.