Solved

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

Posted on 2013-05-22
5
734 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
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:apeter
Comment Utility
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
Comment Utility
Do you have any websites showing a full working solution ?
0
 

Author Comment

by:finance_teacher
Comment Utility
Please reply.
0
 
LVL 25

Accepted Solution

by:
apeter earned 500 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now