显示和外键问题 [英] Display and Foreign Key Problem

查看:79
本文介绍了显示和外键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我是新手,我正在学习一些C#和Code First的MVC方法

我创建类员工和一些其他类来支持它,如下面的代码。







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.



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; }


    }
}
}





现在到了问题,因为我是新手,只是在学习



1.即使我使用显示名称并在运行应用程序上更改它仍然在TitleID下面的屏幕上显示为Fallow GenderID,ShiftID为什么这样做以及我需要纠正什么?





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



2.添加新员工后,所有3个外键都被拉到前面名称是什么,背后的原因,我可以改变吗?







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



这是我对C#的第一种方法MVC只是tr我想在空闲时间学习一些新东西,这就是为什么请求对我来说很容易





最好的问候



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

推荐答案

也应该为Navigation属性类装饰DisplayName属性。

由于所有的Navigation属性类都有Name属性,因此只有通过DisplayName属性指定它才会显示它。

或者应该明确地使用html助手类进行查看。



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...


这篇关于显示和外键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆