Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException Problem with ASP.NET MVC Textbox HtmlHelper

Tags:

asp.net-mvc

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

    }
like image 470
Rich Miller Avatar asked May 05 '09 20:05

Rich Miller


3 Answers

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

like image 61
Craig Stuntz Avatar answered Nov 14 '22 04:11

Craig Stuntz


I came across this as I was typing out a nearly identical question/problem. (ie, rendering my view was causing a mysterious NullReferenceException to to be thrown when the user's value failed validation on a particular field.

A different work-around that I've found is explicitly generating the Html in the view rather than letting the HtmlHelper do the work.

For instance : <%= Html.TextArea("FieldName", Model.FieldName) %> would throw an exception, but <textarea id="FieldName" name="FieldName"><%= Model.FieldName ></textarea> would work perfectly fine.

Thank you for posting the original question since now I'll have to do some more looking into SetModelValue to see which of the two approaches is the better solution...

like image 36
Peter Bernier Avatar answered Nov 14 '22 05:11

Peter Bernier


a link i found helpfull in explaining this http://forums.asp.net/t/1380609.aspx

like image 1
Sjors Miltenburg Avatar answered Nov 14 '22 05:11

Sjors Miltenburg