tags:

views:

2224

answers:

4

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

    }
+9  A: 

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

Craig Stuntz
Craig, thanks for the answer, but I cannot find any SetAttempedValue method on any of the objects in the controller or the view. I see SetModelValue, but it isn't clear to me how to use this -- or even if I should -- given the second parameter of type ValueProviderResult, which I don't know how to construct. The model actually contains the attempted value entered by the user, so I'm not sure where this leaves me. Perhaps deep under the covers somewhere ASP.NET MVC is trying to convert the value to a date and having a problem??
Rich
One way is with Form.ToValueProvider()...
Craig Stuntz
Thanks, Craig. I updated my code to contain the SetModelValue with the Form.ToValueProvider method generating the ValueProviderResult, and this fixed the null reference exception. I'm still confused, but my code works... :-)
Rich
Note that this doesn't work for validation errors assigned to the key "_FORM". You have to test for this key before calling SetModelValue with Form.ToValueProvider(), otherwise you get a key not found exception.
Rich
ModelState.AddModelError("Code", "Unrecognized code"); ModelState.SetModelValue("Code", collection.ToValueProvider("Code"));
Zack Peterson
+1  A: 

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...

Peter Bernier
+1  A: 

This is all very nice and beautiful. Solution works. Now, can anybody explain why this is happening?

Igor Vaschuk
+1  A: 

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

sjors miltenburg