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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:14:33+00:00 2026-05-11T17:14:33+00:00

I have the following code in a strongly-typed View in my application: <td> <label

  • 0

I have the following code in a strongly-typed View in my application:

<td>
<label for="TriageStartDate">Triage Start:</label>
<%= Html.TextBox("TriageStartDate", crit.TriageStartDate, new { maxlength = 10, size = 12 } )%>
<%= Html.ValidationMessage("TriageStartDate", "*") %>
</td>

The variable crit is passed in ViewData and is cast to a strongly-typed object in the view using:

   PatientSearchCriteria crit = (PatientSearchCriteria)ViewData["criteria"];       

The property TriageStartDate is a string property defined on the object like so:

public string TriageStartDate { get; set; }

I have a validation routine in the business object that checks this property for invalid dates. The validation routine is called in my controller, and in the controller I add a ModelError to the ModelState when an invalid date is found (for example “4/34/2009”). This should result in the view being redisplayed and the validation summary and message being displayed also.

However, the code crashes on the Html.TextBox line with an unhandled NullReferenceException. The code only crashes when invalid dates are entered, which probably should not matter since the property is a string anyway, and it should just display the invalid date in the textbox.

Any ideas what may be going on here? I thought it might be an Html.Encode thing, but valid dates display without a fuss. When I break on that line in debug mode, I can see the value “4/34/2009” in the TriageStartDate property, and the crit variable itself is not null, so I am wondering what object IS null?

BTW, the first few lines of the stack trace look like this:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) +63
   System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes) +519
   System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value, IDictionary`2 htmlAttributes) +34
   System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value, Object htmlAttributes) +62

Here is my validation code (which might not be pretty). In the business object:

public override IEnumerable<ValidationError> GetValidationErrors()
{
    // check for valid start date
    if (!String.IsNullOrEmpty(TriageStartDate))
    {
        DateTime critStartDate;
        if (!DateTime.TryParse(TriageStartDate, out critStartDate))
            yield return new ValidationError(String.Format("Invalid triage start date specified: {0}", TriageStartDate), "TriageStartDate");
    }

    // check for valid end date
    if (!String.IsNullOrEmpty(TriageEndDate))
    {
        DateTime critEndDate;
        if (!DateTime.TryParse(TriageEndDate, out critEndDate))
            yield return new ValidationError(String.Format("Invalid triage end date specified: {0}", TriageEndDate), "TriageEndDate");
    }

    // verify that end date follows start date if both are specified
    if (!String.IsNullOrEmpty(TriageStartDate) && !String.IsNullOrEmpty(TriageEndDate))
    {
        DateTime startDate;
        DateTime endDate;

        if (DateTime.TryParse(TriageStartDate, out startDate) && DateTime.TryParse(TriageEndDate, out endDate))
        {
            if (startDate > endDate)
                yield return new ValidationError("Triage start date must be before end date", "_FORM");
        }
    }

    yield break;
}

In the controller:

    // validate search criteria
    if (!criteria.IsValid)
    {
        foreach (ValidationError ve in criteria.GetValidationErrors())
        {
            ModelState.AddModelError(ve.PropertyName, ve.ErrorMessage);
        }
    }

Thanks for any clues!


Thanks to Craig’s suggestions, I updated the controller code as follows, and the null reference exception went away. The solution worked, but I am not sure I understand the rationale, since the value the user attempted to enter is already stored in the model object, and I have other views and controllers in the project that are displaying validation errors in the same manner without any issues. But, hey, if it works…

    // validate search criteria
    if (!criteria.IsValid)
    {
        foreach (ValidationError ve in criteria.GetValidationErrors())
        {
            ModelState.AddModelError(ve.PropertyName, ve.ErrorMessage);
            ModelState.SetModelValue(ve.PropertyName, form.ToValueProvider()[ve.PropertyName]);
        }

    }
  • 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-11T17:14:33+00:00Added an answer on May 11, 2026 at 5:14 pm

    After you call AddModelError you need to call SetModelValue. That should fix the null reference.

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

Sidebar

Related Questions

No related questions found

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.