7

I'd like to know how to change the display name of a model, and customize error messages in Entity Framework. I tried the following but it didn't work.

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

This is the code behind my form that adds data into my database. I've omitted irrelevant 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>
<% } %>
9
  • can you post your view, it would be helpful too Commented Apr 11, 2012 at 14:27
  • Why not try changing from using Edmx to DbContext Code Generation? blogs.msdn.com/b/adonet/archive/2011/09/28/… Hope this helps. Commented Apr 11, 2012 at 14:31
  • @cubski the problem that i want to change dipslaye name, and error message i'm all ready using Data Entity framework(EDMx) Commented Apr 11, 2012 at 15:07
  • 1
    @Chlebta: did you try [DisplayName("Name Agency")] instead of [Display(Name = "Name Agency")] ? Commented Apr 11, 2012 at 15:14
  • What exactly doesn't work? Does Html.ValidationMessageFor(model => model.Nag) not show the error message you have specified in the [Required(...)] attribute when you leave the textbox empty? What message do you see instead? Commented Apr 11, 2012 at 15:17

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

Comments

4

First you need to reference this:

using System.ComponentModel.DataAnnotations;

For changing the display name of the column, actually [Display(Name="Name Agency")] is OK. I'm using it in my projects.

For error message

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

I read that it is possible that this won't work if you are using the entity framework designer because the designer overwrites your changes over and over then you will need to use the metadatatype something 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...
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.