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
Solved

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

Posted on 2013-05-22
5
742 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
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 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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