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

  • SEARCH
  • Home
  • 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 780221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:02:28+00:00 2026-05-14T20:02:28+00:00

I have a simple form on an ASP.NET MVC site that I’m building. This

  • 0

I have a simple form on an ASP.NET MVC site that I’m building. This form is submitted, and then I validate that the form fields aren’t null, empty, or improperly formatted.

However, when I use ModelState.AddModelError() to indicate validation errors from my controller code, I get an error when my view is re-rendered. In Visual Studio, I get that the following line is highlighted as being the location of the error:

<%=Html.TextBox("Email")%>

The error is the following:

NullReferenceException was unhandled by user code – object reference not set to an instance of an object.

My complete code for that textbox is the following:

<p>
<label for="Email">Your Email:</label>
<%=Html.TextBox("Email")%>
<%=Html.ValidationMessage("Email", "*") %>
</p>

Here’s how I’m doing that validation in my controller:

        try
        {
            System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(email);
        }
        catch
        {
            ModelState.AddModelError("Email", "Should not be empty or invalid");
        }

return View();

Note: this applies to all of my fields, not just my Email field, as long as they are invalid.

  • 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-14T20:02:28+00:00Added an answer on May 14, 2026 at 8:02 pm

    That’s a horrible bug/feature (call in whatever) in ASP.NET MVC the helper that you may fix by calling SetModelValue like this:

    ModelState.AddModelError("Email", "Should not be empty or invalid");
    ModelState.SetModelValue("Email", new ValueProviderResult("raw value", "attempted value", CultureInfo.InvariantCulture));
    

    By the way is there any reason you would write all this code when you could simply annotate your view model:

    public class SomeViewModel
    {
        [RegularExpression("Some bulletproof regex you could google to validate email address", ErrorMessage = "Should not be empty or invalid")]
        public string Email { get; set; }
    }
    

    and leave the data binder do the heavy lifting.

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

Sidebar

Related Questions

I have an iPad app that submits orders to an ASP.NET MVC web site
I have an ASP.Net MVC Page with a very simple form on it: One
I have a simple form in ASP.NET MVC. I am trying to post these
I have added a text box to a simple form in ASP.NET MVC and
I have a simple form which is using ASP.NET MVC 3 unobtrusive client side
I have a simple ASP.NET MVC 3 dummy app (just learning MVC coming from
I'm converting a classic ASP page to ASP.NET MVC 2. I have a simple
I have various simple ASP.NET MVC views for CRUD operations which work fine on
I have a simple ASP.Net MVC View which contains an FCKeditor text box (created
I have a situation in ASP.NET MVC 2 where I have a form whose

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.