Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to filter by key press ? 6 56
C# XML Get Values 4 35
Check only one toolstripmenu item 12 28
Fastest way to get file count in MS Server 2012 2 8
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

733 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