Data Entity Framework's display name

asp.net-mvc entity-framework

Question

I want to know how to modify error messages in Entity Framework and alter a model's display name. The things I attempted to do didn't work, however.

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

The code that adds data to my database via my form is shown below. I removed unnecessary lines.

 <% 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>
<% } %>
1
4
5/13/2019 12:10:19 AM

Accepted Answer

Change [Display(Name = "Name Agency")] to [DisplayName("Name Agency")] instead.

9
12/10/2013 11:56:16 AM

Popular Answer

You must first make reference to this:

using System.ComponentModel.DataAnnotations;

In reality, [Display(Name="Name Agency")] is acceptable for modifying the column's display name. It's what I use for my projects.

a warning message

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

According to what I've read, if you're using the entity framework designer and it keeps overwriting your modifications, this may not work. In that case, you'll need to create a metadatatype that looks like this:

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow