Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure I understand the question as well as you're explaining but what I hear you saying is you need a way of looping thru the ModelState errors and then maintaining the text values of the errors but not show them as errors?? Is that what you are saying?</p> <p>Well for starters, ModelState is nothing more than a DictionaryList which you can iterate through with no problem. </p> <p>Something like this can do that:</p> <pre><code>public ActionResult SomeAction(SomeModel model) { if(ModelState.IsValid) { // do cool stuff with model data } var errorMessages = GetModelStateErrors(ModelState); foreach (var errorMessage in errors) { // do whatever you want with the error message string here } } </code></pre> <p>ModelError contains an ErrorMessage property and Exception property</p> <pre><code>internal static List&lt;string&gt; GetModelStateErrors(IEnumerable&lt;KeyValuePair&lt;string, ModelState&gt;&gt; modelStateDictionary) { var errors = new List&lt;ModelError&gt;(); errors = modelStateDictionary.SelectMany(item =&gt; item.Value.Errors).ToList(); } </code></pre> <p>Not sure if this helps or not but if it points you in the right direction then cool :-)</p> <p><strong>Update</strong></p> <p>Ok here is what I came up with and it works for me with my test app.</p> <p>First let me lay out what I have so you can copy and replicate</p> <p><strong>Here's my model</strong></p> <pre><code>public class EmployeeViewModel { public int ID { get; set; } [Display(Name = "First Name")] [Required(ErrorMessage = "Error")] public string FirstName { get; set; } [Display(Name = "Last Name")] [Required(ErrorMessage = "Error")] public string LastName { get; set; } [Display(Name = "Username")] public string Username { get; set; } [Display(Name = "Email Address")] public string EmailAddress { get; set; } } </code></pre> <p><strong>Here's a simple view that uses this model</strong></p> <pre><code>@model TestApp.Models.EmployeeViewModel &lt;h2&gt;Test&lt;/h2&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;EmployeeViewModel&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.FirstName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.FirstName) @Html.ValidationMessageFor(model =&gt; model.FirstName) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.LastName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.LastName) @Html.ValidationMessageFor(model =&gt; model.LastName) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Username) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Username) @Html.ValidationMessageFor(model =&gt; model.Username) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.EmailAddress) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.EmailAddress) @Html.ValidationMessageFor(model =&gt; model.EmailAddress) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p><strong>Here's the controller and actions that I used</strong></p> <pre><code>using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using TestApp.Models; namespace TestApp.Controllers { public class HomeController : Controller { public ActionResult Index() { return RedirectToAction("Test"); } public ActionResult Test() { var model = new EmployeeViewModel(); return View(model); } [HttpPost] public ActionResult Test(EmployeeViewModel model) { // Force an error on this property - THIS should be the only real error that gets returned back to the view ModelState.AddModelError("", "Error on First Name"); if(model.EmailAddress == null) // Add an INFO message ModelState.AddModelError("", "Email Address Info"); if (model.Username == null) // Add another INFO message ModelState.AddModelError("", "Username Info"); // Get the Real error off the ModelState var errors = GetRealErrors(ModelState); // clear out anything that the ModelState currently has in it's Errors collection foreach (var modelValue in ModelState.Values) { modelValue.Errors.Clear(); } // Add the real errors back on to the ModelState foreach (var realError in errors) { ModelState.AddModelError("", realError.ErrorMessage); } return View(model); } private IEnumerable&lt;ModelError&gt; GetRealErrors(IEnumerable&lt;KeyValuePair&lt;string, ModelState&gt;&gt; modelStateDictionary) { var errorMessages = new List&lt;ModelError&gt;() ; foreach (var keyValuePair in modelStateDictionary) { if (keyValuePair.Value.Errors.Count &gt; 0) { foreach (var error in keyValuePair.Value.Errors) { if (!error.ErrorMessage.Contains("Info")) { errorMessages.Add(error); } } } } return errorMessages; } } } </code></pre> <p><strong>You can write GetRealErrors as LINQ instead if you prefer:</strong></p> <pre><code>private IEnumerable&lt;ModelError&gt; GetRealErrors(IEnumerable&lt;KeyValuePair&lt;string, ModelState&gt;&gt; modelStateDictionary) { var errorMessages = new List&lt;ModelError&gt;() ; foreach (var keyValuePair in modelStateDictionary.Where(keyValuePair =&gt; keyValuePair.Value.Errors.Count &gt; 0)) { errorMessages.AddRange(keyValuePair.Value.Errors.Where(error =&gt; !error.ErrorMessage.Contains("Info"))); } return errorMessages; } </code></pre> <p>I hope that gives you what you were looking for, it works the way I think I understand that you wanted. Let me know</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload