0

I am learning and I am trying to change the parameter name of my autogenerated partial class using [Display()] attribute but I am getting this error message.

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'Student_Age' could not be found (are you missing a using directive or an assembly reference?) EFScuffolding C:\Users\kusha\source\repos\EFModel\EFScuffolding\Models\Students.cs 20 Active

This is my autogenerated code for Student Class

            namespace EFDemo.Models
            {
                using System;
                using System.Collections.Generic;

                public partial class Student
                {
                    public int StudentID { get; set; }
                    public string Student_Name { get; set; }
                    public string Student_Major { get; set; }
                    public Nullable<int> Student_Age { get; set; }
                }
            }

This the class I am using to modify the parameters for display

        using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Web;
        using EFScuffolding.Models;

            namespace EFDemo.Models
            {
                [MetadataType(typeof(StudentMetaData))]
                public partial class Student
                {

                }
                public class StudentMetaData
                {
                    [Required]
                    public int StudentID { get; set; }
                    [Required]
                    [Display(Student_Age = "Student Name")]
                    public string Student_Name { get; set; }
                    [Required]`enter code here`
                    [Display(Student_Major = "Student Major")]
                    public string Student_Major { get; set; }
                    [Required]
                    [Display(Student_Age = "Student Age")]
                    public Nullable<int> Student_Age { get; set; }

                }
            }

I am using MVC 5 ,EF6 and Visual Studio 17. Am I missing anything in the code.

Kushal
  • 17
  • 7
  • I used ADO.NET entity model to generate the code for the model and I am able to use the validation on the parameters using [Required].I didn't face any problem with this.But I am getting the error only for [Display] attribute.I am able to retrieve data and display without any issue.I am unable to change the display names though, – Kushal Dec 17 '17 at 06:14

1 Answers1

0

Your not using the DisplayAttribute correctly. You need to set its Name property to the text you want to display

[Display(Name = "Student Name")] // not Student_Age = "Student Name"
public string Student_Name { get; set; }

and ditto for the other 2 properties

However, in asp.net-mvc, use view models, not partial classes. Refer What is ViewModel in MVC?