简体   繁体   中英

MVC 5 data annotations not scaffolding

I created a MVC project using Code First From A Database. I added data annotations to my Model classes, eg [DisplayName("Ruling Request ID")] and [ScaffoldColumn(false)].

I selected Add MVC 5 Controller with views, using Entity Framework, selected my Model Class, Data context class, and left Generate Views, Reference script libraries, and Use a layout page checked. All of the files and folders are added correctly.

My problem occurs when I run the app. The index, details, and edit views all display my long property names and not the names I gave the properties using the DisplayName data annotation. Plus, the properties I declared as ScaffoldColumn(false) are appearing on all 3 views.

I'm pasting the code from one of my Model classes below:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
using System.ComponentModel;


[Table("RulingRequest")]
public partial class RulingRequest
{

[DatabaseGenerated(DatabaseGeneratedOption.None)]
[DisplayName("Ruling Request ID")]
public int RulingRequestID { get; set; }

[StringLength(9)]
[ScaffoldColumn(false)]
public string CreatedBy { get; set; }

[DisplayName("Ruling Request Group ID")]
[StringLength(3)]
public string RulingRequestGroupID { get; set; }  //FK

[DisplayName("Type")]
public int? RulingRequestTypeID { get; set; }  //FK

[StringLength(50)]
[DisplayName("First Name")]
public string FirstName { get; set; }

I'm a newbie and have searched the interwebs for help, but I haven't come across a similar situation as mine.

Try this instead: [Display(Name = "Ruling Request ID")]

Also try this to prevent binding:

[Bind(Exclude = "CreatedBy")]
public partial class RulingRequest
{

I suggest first to check whether you have included the necessary javascript files for unobtrusive validation in the right order and that will make the validation happen successfully at the client-side. check the answer given here

You must supply the ScaffoldColumnAttribute to the model property prior to creating your view. The scaffolding will not create a DisplayFor/EditFor for that particular property and any other properties so attributed. If you provide the attribute post-creation, the properties will still be displayed in the form as there is already a DisplayFor/EditFor Html Helper created for that model property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM