Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:45:05+00:00 2026-06-05T03:45:05+00:00

I want to know, is there a way that I can pass an object(let’s

  • 0

I want to know, is there a way that I can pass an object(let’s say a Message instance) from the validation in the model’s fields and retrieve them from the ModelState( or by using some other thing) instance. Why I’m asking this is, I want to differentiate between the validation error message types so I can display only the messages I want in the view at a time. (Ex : required messages shown before the unique validation messages.)

I was trying to use a custom created Message object which I can then distinguish using its messageType field. But as the validation only returns string messages, can’t think of a way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:45:06+00:00Added an answer on June 5, 2026 at 3:45 am

    Are you ok with reading the messages out of ModelState and determining type based on message content? That can be done if you set a custom message for all the validations.

    Then you can evaluate each message looking for specific content and take action. Such as putting the word “Error” in the Required attribute and Info in the rest.

    Here’s a class you can use to test

    Model


    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; }
    }
    

    Controller


    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<ModelError> GetRealErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
                var errorMessages = new List<ModelError>() ;
                foreach (var keyValuePair in modelStateDictionary.Where(keyValuePair => keyValuePair.Value.Errors.Count > 0)) {
                    errorMessages.AddRange(keyValuePair.Value.Errors.Where(error => !error.ErrorMessage.Contains("Info")));
                }
                return errorMessages;
            }
        }
    }
    
    private IEnumerable<ModelError> GetRealErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
        var errorMessages = new List<ModelError>() ;
        foreach (var keyValuePair in modelStateDictionary.Where(keyValuePair => keyValuePair.Value.Errors.Count > 0)) {
            errorMessages.AddRange(keyValuePair.Value.Errors.Where(error => !error.ErrorMessage.Contains("Info")));
        }
        return errorMessages;
    }
    

    View


    @model TestApp.Models.EmployeeViewModel
    
    <h2>Test</h2>
    
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>EmployeeViewModel</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.LastName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Username)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Username)
                @Html.ValidationMessageFor(model => model.Username)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know is there any way that I can remove duplicates from
I want to know is there any way or message in MFC by which
I want to know if there is any way by which I can paste
I know that I can pass object values through a URL pattern and use
I want to know if there is a better way (than what I'm currently
All I want to know is if there's an easy way to add lines
I know the class name, say MyClass and want to retrieve the Class object,
I want to pass an object from my c# code behind to my javascript.
I want to pass a row object to a function that uses the configuration
i want to know there a limitation in size for an iphone to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.