?
Solved

ASP.Net MVC 4 C# -- custom error message fails ?

Posted on 2013-05-22
5
Medium Priority
?
750 Views
Last Modified: 2013-05-28
ASP.Net MVC 4 C# -- custom error message fails ?

I added StringLength(10, ErrorMessage="10 chars or less")] to the below two pages like
http://stackoverflow.com/questions/14059455/adding-validation-attributes-with-an-entity-framework-data-model
talked about, REBUILD, ran the app, make a change, click SAVE, and get the attached error, not "10 chars or less"

How can I get "10 chars or less" to display ?
------------------------------------------
\Models\MD_Table1.cs

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

namespace MvcApplication18.Models
{
    internal sealed class MD_Table1
    {
        [Required(ErrorMessage = "Test1 is required.")]
        [StringLength(10, ErrorMessage = "10 chars or less")]
        public string Test1;
    }
}
------------------------------------------
\Models\Table1.cs


// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>

namespace MvcApplication18.Models
{
    using System;
    using System.Collections.Generic;
    [System.ComponentModel.DataAnnotations.MetadataType(typeof(MD_Table1))]
    public partial class Table1
    {
        public int ID { get; set; }
        public string Test1 { get; set; }
    }
}
------------------------------------------
s002.jpg
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 39190524
You should have a separate catch block to catch validation errors. Modify accordingly as per  your need
try
{
...
...
db.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}
0
 

Author Comment

by:finance_teacher
ID: 39190751
Do you have any websites showing a full working solution ?
0
 

Author Comment

by:finance_teacher
ID: 39200054
Please reply.
0
 
LVL 25

Accepted Solution

by:
apeter earned 2000 total points
ID: 39200478
I don't have a fully solution but you can create  string from insetad console.writeline....and pass to client to display the details

string validationMsg;
foreach (var eve in e.EntityValidationErrors)
    {
       
           validationMsg = string.format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            validationMsg  += string.format("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
0
 

Author Comment

by:finance_teacher
ID: 39202473
Steps
1.      Create \Models\MD_POheader.cs with below YELLOW

using System.ComponentModel.DataAnnotations;

namespace MvcApplication21.Models
{
    internal sealed class MD_POheader    {
        [Required(ErrorMessage = "Test1 is required.")]
        [StringLength(10, ErrorMessage = "10 chars or less")]
        public string LongName { get; set; }
    }
}

2.      Add below above generated \Models\Model1.tt\POHeader.cs public partial class POheader
•      [System.ComponentModel.DataAnnotations.MetadataType(typeof(MD_POheader))]
3.      WORKS
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question