2

I created a MVC project using Code First From A Database. I added data annotations to my Model classes, e.g. [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.

Vijay Chavda
  • 826
  • 2
  • 15
  • 34
MikeNaka
  • 21
  • 5
  • 1
    Post one of the views – IndieTech Solutions Apr 16 '15 at 17:12
  • Stop using data models in your view and start using view models. [What is a View Model in MVC](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Apr 17 '15 at 01:49
  • I tried adding an image of one of the views, but I got an error when trying to save my edited question- you must have a reputation of at least 10 to post an image. – MikeNaka Apr 17 '15 at 16:44

3 Answers3

0

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

Also try this to prevent binding:

[Bind(Exclude = "CreatedBy")]
public partial class RulingRequest
{
IndieTech Solutions
  • 2,527
  • 1
  • 21
  • 36
  • I tried [Display(Name = "Ruling Request ID")], but it didn't work. Thanks for the idea though! The weird part is that my [ScaffoldColumn(false)] is now working. – MikeNaka Apr 17 '15 at 17:32
  • It should work . the problem is not the annotation then . make sure unobtrusive java is enabled on your page – IndieTech Solutions Apr 17 '15 at 18:29
  • I added jquery.validate and jquery.validate.unobtrusive to BundleConfig and the Shared view, _Layout.cshtml, via Scripts.Render. I checked the Details web page source code, and both scripts are shown. It's strange b/c some of my properties are displaying what I have as Display Name, e.g. First Name, Last Name, Address 2, Decision Date, etc... I appreciate your comments as I hadn't added those 2 scripts to the Scripts.Render function. – MikeNaka Apr 17 '15 at 19:32
  • so the annotation is working for all other attributes except for rulingrequestID? – IndieTech Solutions Apr 17 '15 at 19:36
  • As of now, all of the annotations are working. Your comments provided me with the answer for why most of my annotations were not being displayed correctly. To get the Foreign Keys to display correctly, I had to add annotations to those model class files. – MikeNaka Apr 22 '15 at 16:06
0

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

Community
  • 1
  • 1
Raas Masood
  • 1,475
  • 3
  • 23
  • 61
0

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.

Bill C.
  • 11
  • 4