Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I'm new to this all and I'm learning some C# and Code First approach to MVC
I create Class Employee and some additional classes to support it like on code below.



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NEMS_Project.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;


namespace NEMS.Models
{
    public class Employee
    {
        [Key]
        public virtual int EmployeeID { get; set; }

        [ForeignKey("TitleID")]
        public virtual Title Title { get; set; }

        [Display(Name = "Title")]
        public virtual int TitleID { get; set; }

        [Display(Name = "First Name")]
        public virtual string FirstName { get; set; }
        [Display(Name = "Last Name")]
        public virtual string LastName { get; set; }

        [Display(Name = "Date of Birth")]
        public virtual DateTime DOB { get; set; }

        [ForeignKey("GenderID")]
        public virtual Gender Gender { get; set; }

        [Display(Name = "Gender")]
        public virtual int GenderID { get; set; }

        [Display(Name = "Adress Line 1")]
        public virtual string AddressLine1 { get; set; }
        [Display(Name = "Adress Line 2")]
        public virtual string AddressLine2 { get; set; }

        public virtual string City { get; set; }

        [Display(Name = "Post Code")]
        public virtual string PostCode { get; set; }

        [Display(Name = "Email")]
        public virtual string EmailAddress { get; set; }

        [Display(Name = "Phone Nr")]
        public virtual int Phone { get; set; }

        [Display(Name = "Employment Nr")]
        public virtual int EmploymentNr { get; set; }

        [Display(Name = "Start Date")]
        public virtual DateTime HireDate { get; set; }

        [Display(Name = "Holiday Total")]
        public virtual int HolidayTotal { get; set; }

        [Display(Name = "Sick Leave Total")]
        public virtual int SickLeaveTotal { get; set; }

        [ForeignKey("ShiftID")]

        public virtual Shift Shift { get; set; }

        [Required]
        [Display(Name = "Shift")]
        public virtual int ShiftID { get; set; }



    }

    public class Title
    {
        public virtual int TitleID { get; set; }
        public virtual string Name { get; set; }

    }
    public class Gender
    {
        public virtual int GenderID { get; set; }
        public virtual string Name { get; set; }
    }
    public class Shift
    {
        public virtual int ShiftID { get; set; }
        public virtual string Name { get; set; }
        public virtual TimeSpan StartTime { get; set; }
        public virtual TimeSpan EndTime { get; set; }


    }
}
}


Now to the problem as I'm new and just learning

1. Even when I use Display Name and change it on Running app its showing still as Fallow on The screen below TitleID, GenderID, ShiftID why is doing that and what i need to correct ?


http://x80i.img-up.net/screen24360.gif[^]

2. After Adding New Employee all 3 foreign key are pulled on front and the are called Name whats the reason behind and can I change it ?



http://v78i.img-up.net/Screen8806.gif[^]

Its my First approach to C# and MVC just trying to learn something new in free time that's why pleas go easy on me


Best Regards
Posted
Comments
JoCodes 15-Dec-13 23:34pm    
Would suggest you to refer the link http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application
Since the way the dependent classes are modelled in your code can be changed accordingly . Just for your learning but out the context since this not related your particular issue.
SebSCO 16-Dec-13 17:25pm    
Grate stuff Thank You Sir :) it was really good lecture :)
JoCodes 17-Dec-13 5:43am    
My Pleasure :). Have a great learning and all the very best...

1 solution

Should decorate DisplayName property for the Navigation property classes too.
Since all your Navigation property classes have Name property it will display that only unless you specify it through the DisplayName attribute.
Or should be done explicitly View using html helper class.

@Html.LabelFor(model => model.Name, "Display Name")


Both way it works.

Hope this helps...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900