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 746263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:04:15+00:00 2026-05-14T14:04:15+00:00

I am trying to validate a form in MVC. I add custom errors to

  • 0

I am trying to validate a form in MVC.

I add custom errors to model state and get it as invalid when form is submitted. When the view is displayed it doesn’t show validation messages nor validation summary. Can anyone please let me know what am I doing wrong or point me in the right direction if there is any other way of validating?

Edit This is ASP.NET MVC 1. Here’s the code:

Following is the entity

namespace DCS.DAL.Entities
{
    public class Group : IDataErrorInfo
    {
        public int GroupId { get; set; }

        public string GroupName { get ; set; }
        public string AboutText { get; set; }
        public string LogoURL { get; set; }
        public string FriendlyURL { get; set; }
        public bool ExcludeFromFT { get; set; }
        public ContactInfo ContactInfo { get; set; }

        public string Error { get { return string.Empty;  } }

        public string this[string propName]
        {
            get
            {
                if ((propName == "GroupName") && string.IsNullOrEmpty(GroupName))
                    return "Please enter Group Name";
                return null;
            }
        }


    }
}

Following is the view

<%= Html.ValidationSummary("Please correct following details") %>  

    <% using (Html.BeginForm()) {%>

    <div id="divError" Style="display:none;">
        errors 
        <%
                foreach (KeyValuePair<string, ModelState> keyValuePair in ViewData.ModelState)
                {
                    foreach (ModelError modelError in keyValuePair.Value.Errors)
                    {
            %> 
                         <% Response.Write(modelError.ErrorMessage); %>
            <%
                    }
                }
            %>   
    </div>

        <fieldset>

            <table>
                <tr>
                    <td>
                        <label for="GroupName">Group Name:</label>
                    </td>
                    <td>
                        <%= Html.TextBox("GroupName", Model.GroupName) %>
                        <%= Html.ValidationMessage("GroupName","group") %>
                    </td>

Foreach loop is for testing, it does gets into the for loop but doesn’t response.write error message nor validation summary nor validation message.

Following is the controller

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditGroup(Group group, FormCollection collection)
        {
            //Group group = new Group();
            bool success = false;
            try
            {

                    var contactInfo = new ContactInfo
                                          {

                                              ContactName = collection["ContactName"],
                                              Email = collection["Email"],
                                              Fax = collection["Fax"],
                                              HeadOfficeAddress = collection["HeadOfficeAddress"],
                                              Freephone = collection["Freephone"],
                                              Telephone = collection["Telephone"],
                                              Website = collection["Website"]
                                          };

                group.ContactInfo = contactInfo;
                group.GroupName = collection["GroupName"];
                if(string.IsNullOrEmpty(group.GroupName))
                {
                    ModelState.AddModelError("GroupName", "Please enter group name");

                }

                if (!ModelState.IsValid)
                {
                    success = groupRepository.InsertUpdateGroup(group);
                    return View(group);
                }

            }
            catch
            {

            }

            //return Json(success);
            return View(group);
        }

It does go into the if(!Modelstate.isvalid) loop but it doesn’t display error.

Edit 2 I can see in the Text Visualiser that the Validation Summary does have the error message, it wont display on screen though.

Thanks

  • 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-05-14T14:04:15+00:00Added an answer on May 14, 2026 at 2:04 pm

    You could decorate your model properties with data annotation attributes allowing you to perform some validation logic. Here’s a simplified example:

    Model:

    public class Group
    {
        [Required]
        public string GroupName { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new Group());
        }
    
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(Group group)
        {
            // Remark: You don't need the FormCollection as argument to this action,
            // leave the default model binder do the job - it will also work
            // for the ContactInfo property as long as you name your text fields 
            // appropriately. For example Html.TextBox("ContactInfo.Email", Model.ContactInfo.Email)
            return View(group);
        }
    }
    

    View:

    <% using (Html.BeginForm()) { %>
        <label for="GroupName">Group Name:</label>
        <%= Html.TextBox("GroupName", Model.GroupName) %>
        <%= Html.ValidationMessage("GroupName", "group") %>
        <input type="submit" value="Post" />
    <% } %>
    

    It’s up to you to decide whether Data Annotations are sufficient for your case, but bear in mind that if you need to perform more advanced validation scenarios you might take a look at third party frameworks like FluentValidation and xVal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write test harness for part of my Android mapping application. I
I'm trying to build a C++ extension for python using swig. I've followed the
I'm trying to build a Chrome browser extension, that should enhance the way the
I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME
I am trying to load a html page through UIWebview.I need to disable all
I am trying to understand the practical difference during the execution of a program
I am playing with TFS 2010, and am trying to setup a build process
I have several USB mass storage flash drives connected to a Ubuntu Linux computer

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.