如何在MVC中更改显示的数据库字段名称? [英] How can I change the displayed database field names in MVC?

查看:203
本文介绍了如何在MVC中更改显示的数据库字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC模型,其中大多数字段名称是单字母字符串,因为我的客户端所需的列标题是长度最多约15个字的句子。在数据库中,我保持字段名称较小,因此我可以添加计算字段(数据都是小整数值)。我将一个和两个字母的字段名称从Excel传送到SQL Server,因为Excel是原始的数据库。



如下所示,我已经添加了DataAnnotations命名空间,并使用Display指令更改MVC视图中显示的字段名称,就像我在培训视频中看到的那样。我在下面显示两个字段,但实际上,有许多这样的字段,以及许多这样的表。与训练视频中的所有内容始终完美无缺,显示(')后面的单字母字段名称给出了一个错误,表示'DisplayAttribute'的最佳重载没有名为'G'的参数。它说第二个'H'等等。



导致此错误的原因是什么?我如何得到此错误,标记为在'显示'之后的每个字段名称,要离开?我有21个不同的表格。我希望我不需要使用超长字段名称来修改整个数据库。感谢您的帮助。



我尝试过:



使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.ComponentModel.DataAnnotations;



命名空间MyProject.Models

{

[元数据类型(typeof(hraInMetaData))]

公共部分班级hraIn

{

}



公共类hraInMetaData

{

[显示( G:长字段名称1)]

public Nullable< byte> G {得到;组; } b / b
[显示(H:长字段名称2)]

public Nullable< byte> H {得到;组; }







I have an MVC model in which most of the field names are single-letter strings because my client's desired column headers are sentences of up to about 15 words in length. In the database, I kept the field names small so I could add calculated fields (the data is all small integer values). I kind of carried the one and two letter field names over from Excel to SQL Server, as Excel was the original 'database'.

As you can see below, I've added the DataAnnotations namespace and am using the Display directive to change the field names displayed in the MVC views, exactly as I have seen done in training videos. I'm showing two fields below, but actually, there are many such fields, as well as many such tables. Unlike in the training video where everything always works perfectly, the single-letter field names following 'Display(' are each giving me an error that says "The best overload for 'DisplayAttribute' does not have a parameter named 'G'". It says 'H' for the second one and so on.

What is causing this error? How can I get this error, marked on each field name after 'Display(', to go away? I have 21 different tables like this. I hope I don't have to revamp the entire database with super long field names. Thanks for your help.

What I have tried:

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

namespace MyProject.Models
{
[MetadataType(typeof(hraInMetaData))]
public partial class hraIn
{
}

public class hraInMetaData
{
[Display(G: "Long Field Name 1")]
public Nullable<byte> G { get; set; }

[Display(H: "Long Field Name 2")]
public Nullable<byte> H { get; set; }
.
.
.
.

推荐答案

尝试添加如下数据注释:[Display(Name =G)]
Try adding your data annotation like this: [Display(Name="G")]


这篇关于如何在MVC中更改显示的数据库字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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