CopyPastor

Detecting plagiarism made easy.

Score: 0.766941249370575; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2019-01-04
by NaDeR Star

Original Post

Original - Posted on 2015-06-17
by CodeArtist



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I have one dirty solution.
public static class ModelStateErrorHandler { /// <summary> /// Returns a Key/Value pair with all the errors in the model /// according to the data annotation properties. /// </summary> /// <param name="errDictionary"></param> /// <returns> /// Key: Name of the property /// Value: The error message returned from data annotation /// </returns> public static Dictionary<string, string> GetModelErrors(this ModelStateDictionary errDictionary) { var errors = new Dictionary<string, string>(); errDictionary.Where(k => k.Value.Errors.Count > 0).ForEach(i => { var er = string.Join(", ", i.Value.Errors.Select(e => e.ErrorMessage).ToArray()); errors.Add(i.Key, er); }); return errors; } public static string StringifyModelErrors(this ModelStateDictionary errDictionary) { var errorsBuilder = new StringBuilder(); var errors = errDictionary.GetModelErrors(); errors.ForEach(key => errorsBuilder.AppendFormat("{0}: {1} -", key.Key,key.Value)); return errorsBuilder.ToString(); } }
For just in case someone need it i made and use the following static class in my projects
**Usage example:**
if (!ModelState.IsValid) { var errors = ModelState.GetModelErrors(); return Json(new { errors }); }


**Usings:**
using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using WebGrease.Css.Extensions;
**Class:**
public static class ModelStateErrorHandler { /// <summary> /// Returns a Key/Value pair with all the errors in the model /// according to the data annotation properties. /// </summary> /// <param name="errDictionary"></param> /// <returns> /// Key: Name of the property /// Value: The error message returned from data annotation /// </returns> public static Dictionary<string, string> GetModelErrors(this ModelStateDictionary errDictionary) { var errors = new Dictionary<string, string>(); errDictionary.Where(k => k.Value.Errors.Count > 0).ForEach(i => { var er = string.Join(", ", i.Value.Errors.Select(e => e.ErrorMessage).ToArray()); errors.Add(i.Key, er); }); return errors; }
public static string StringifyModelErrors(this ModelStateDictionary errDictionary) { var errorsBuilder = new StringBuilder(); var errors = errDictionary.GetModelErrors(); errors.ForEach(key => errorsBuilder.AppendFormat("{0}: {1} -", key.Key,key.Value)); return errorsBuilder.ToString(); } }

        
Present in both answers; Present only in the new answer; Present only in the old answer;