【问题标题】:Display name in Data Entity framework在数据实体框架中显示名称
【发布时间】:2019-05-13 08:10:19
【问题描述】:

我想知道如何更改模型的显示名称,并在实体框架中自定义错误消息。我尝试了以下方法,但没有成功。

    [Required(ErrorMessage = "Required .... :")]
    [Display(Name = "Name Agency : ")]
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String Nag
    {
        get
        {
            //code
        }
        set
        {
           //code
        }
    }

这是我的表单背后的代码,用于将数据添加到我的数据库中。我省略了不相关的行。

 <% using (Html.BeginForm("addcar", "Agence", FormMethod.Post, new { @class = "search_form" }))
   { %>
    <%: Html.ValidationSummary(true) %>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Dmcv) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.Dmcv) %>
            <%: Html.ValidationMessageFor(model => model.Dmcv) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Puisv) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.Puisv) %>
            <%: Html.ValidationMessageFor(model => model.Puisv) %>
        </div>

        // Similaire code

        <p>
            <input type="submit" value="Create" />
        </p>
<% } %>

【问题讨论】:

  • 你能发表你的看法吗,也很有帮助
  • 为什么不尝试从使用 Edmx 更改为 DbContext 代码生成? blogs.msdn.com/b/adonet/archive/2011/09/28/…希望这会有所帮助。
  • @cubski 我想更改 dipslaye 名称的问题,以及错误消息我已经准备好使用数据实体框架(EDMx)
  • @Chlebta:你试过 [DisplayName("Name Agency")] 而不是 [Display(Name = "Name Agency")] 吗?
  • 究竟是什么不起作用?当您将文本框留空时,Html.ValidationMessageFor(model =&gt; model.Nag) 是否不会显示您在 [Required(...)] 属性中指定的错误消息?你看到的是什么信息?

标签: asp.net-mvc entity-framework


【解决方案1】:

[Display(Name = "Name Agency")]改为[DisplayName("Name Agency")]

【讨论】:

    【解决方案2】:

    首先你需要参考这个:

    using System.ComponentModel.DataAnnotations;
    

    对于更改列的显示名称,实际上[Display(Name="Name Agency")] 是可以的。我在我的项目中使用它。

    对于错误信息

    [Required(ErrorMessage="Required...")]
    

    我了解到,如果您使用实体框架设计器,这可能不起作用,因为设计器会一遍又一遍地覆盖您的更改,那么您将需要使用如下元数据类型:

    [MetadataType(typeof(MetadataMyClass))]
    public partial class myclass
    {
    }
    
    //data annotations here
    public class MetadataMyClass
    {
      [Required(ErrorMessage = "Required...")]
      [Display(Name="Column Name")]
      public global:: System.String Nag
      {
        // ... etc, etc...
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多