Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改外键MVC的显示名称_C#_Asp.net Mvc_Asp.net Mvc 5_Entity Framework 6 - Fatal编程技术网

C# 更改外键MVC的显示名称

C# 更改外键MVC的显示名称,c#,asp.net-mvc,asp.net-mvc-5,entity-framework-6,C#,Asp.net Mvc,Asp.net Mvc 5,Entity Framework 6,在我的MVC应用程序中,我有一个属于特定“类别”的型号,另一个型号。 当显示索引视图时,此模型的名称和类别名称都称为“名称”,如何将类别的显示名称更改为名称以外的名称 [Table("Product")] public partial class Product { public int ProductId { get; set; } [Required] public string Name { get; set; }

在我的MVC应用程序中,我有一个属于特定“类别”的型号,另一个型号。
当显示索引视图时,此模型的名称和类别名称都称为“名称”,如何将类别的显示名称更改为名称以外的名称

[Table("Product")]
    public partial class Product
    {
        public int ProductId { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Description { get; set; }

        public int CategoryId { get; set; }

        public decimal Price { get; set; }

        public virtual Category Category { get; set; }
    }
我尝试过在CategoryId和Category上面使用
[DisplayName(“新名称”)]
,但这似乎不起作用,有人能建议我吗


谢谢

当您使用脚手架生成视图时,可能会发生这种情况。它发生在使用下拉列表选择外键的地方。与其他生成的标签不同,这些标签传递了一个附加参数“labelText”,该参数将覆盖在模型属性中设置的显示名称。删除附加参数或将其更改为要显示的文本。我在下面的示例代码中添加了文本(ChangeOrRemoveMe)

    <div class="form-group">
        @Html.LabelFor(model => model.CategoryID, "CategoryID(ChangeOrRemoveMe)", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
        </div>
    </div>

@Html.LabelFor(model=>model.CategoryID,“CategoryID(changeorremovem)”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“CategoryID”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoryID,“,new{@class=“text danger”})

您正在使用任何其他映射文件吗?不清楚您想要什么,但是如果您使用[Display(Name=“New Name”)]public int CategoryId{get;set;}`并且在视图
@Html.LabelFor(m=>m.CategoryId)
中,它将呈现“New Name”