[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-05-22
5
Medium Priority
?
756 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

650 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