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 8081905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:53:38+00:00 2026-06-05T16:53:38+00:00

I want DRY/reuse as much editor code (View and Model) as possible. Some of

  • 0

I want DRY/reuse as much editor code (View and Model) as possible. Some of my fields can only be set at creation, and never edited. Are there any pre-existing MVC/DataAnnotation features I should look at?

For example, maybe there is a data attribute that causes EditorFor to operate like DisplayFor if the value is non-null.

Model.cs

[Unchangeable]
string UserReferenceId { get; set; }

string Description { get; set; }

edit: to clarify my goal, I’ve added an answer with sample code for the approach I’m currently planning. If there’s a better way/pre-existing feature for this, please let me know.

  • 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-05T16:53:40+00:00Added an answer on June 5, 2026 at 4:53 pm

    There are both the System.ComponentModel.ReadOnlyAttribute and System.ComponentModel.DataAnnotations.EditableAttribute (I think EditableAttribute is .NET 4). When model metadata is created for properties marked with either of these, you can see ModelMetadata.IsReadOnly will be set correctly.

    Frustratingly, however, the built-in editor templates will still show editable fields, even if ModelMetadata.IsReadOnly is true.

    You can, however, create your own shared editor template for each data type where you want this metadata property respected, and handle it specifically.

    ~/Views/Shared/EditorTemplates/String.cshtml

    @model String
    @if (ViewData.ModelMetadata.IsReadOnly)
    {
      @Html.Hidden(string.Empty, Model)
    }
    @(ViewData.ModelMetadata.IsReadOnly ? Html.DisplayText(string.Empty) : Html.TextBox(string.Empty))
    

    View Model

    [Editable(false)]
    public string UserReferenceId { get; set; }
    

    You’ll note that in the event the metadata for the model indicates IsReadOnly, I draw a hidden field. This is so the value of that property is persisted across posts.

    If you don’t want the field displayed at all, but persisted across posts, you can use System.Web.Mvc.HiddenInputAttribute. In this case, only the hidden is drawn.

    [HiddenInput(DisplayValue=false)]
    public string UserReferenceId { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to DRY my source code. Let's say I had some of these
i'm trying to follow DRY and i've got some functions i have to reuse.
Here is a snippet of my JavaScript Code that I want to DRY up:
When I want to apply the DRY principle, i.e. to unify the code of
I just want to be as DRY as possible. $(document).ready(function(){ var w= $(window).width(); w-=
want to have a Hyperlink-Button in a gridView in which I can display a
Want to code a key pad for an calculator. What I want to make
In an attempt to DRY up my code today i'd like to do the
I am trying to DRY up my code a bit so I am writing
I am trying to DRY up a bunch of forms code that has a

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.