Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Razor Pages Html.EditorFor Default Value

$
0
0

Noob here regarding Razor Pages / MVC. Was trying to figure out how to do "down and dirty" editing on some of my models.

Let's say my data model is defined as:

publicclassBook{publicintId{get;set;}=0;[Required][MaxLength(75)][Display(Name="Book Title")]publicstringTitle{get;set;}[Required][MaxLength(50)]publicstringAuthor{get;set;}[Required][Range(0,99)]publicdecimalPrice{get;set;}[Required][Range(0,5)]publicintRank{get;set;}}

I found that if is use:

@Html.EditorFor(model => model.Book)

...on my .cshtml (edit or create) page, I can get a basic edit or create screen that respects validation and other attributes for all fields in a model. Great! I could use this for basic admin data editing.

The problem is twofold:

  1. On my Edit page, I need my "Id" field to be read only
  2. On my Create page, I need my "Id" field to default to 0 and be read only

Any combination of:

//[ScaffoldColumn(false)]//[HiddenInput(DisplayValue =false)]//[HiddenInput]//[DefaultValue(0)]//[ReadOnly(true)]//[Editable(false)]

...seemingly does not work.

Is there a way to accomplish my two requirements listed above?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>