I am getting a System.NullReferenceException: Object reference not set to an instance of an object when ever I hit “back” on a wizard I am using (also, I am getting a “confirm resubmission” page if I just hit back on the browser). This is due to the following code (normally, without using this system hitting “back” on my wizard or the back button on the browser works fine):
// Loop through the items and make sure they are Selected if the value has been posted
if(Model != null)
{
foreach (var item in selectorModel.Items)
{
if (supportsMany)
{
var modelStateValue = GetModelStateValue<string[]>(Html, fieldName) ?? ((IEnumerable)Model).OfType<object>().Select(m => m.ToString());
item.Selected = modelStateValue.Contains(item.Value);
}
else
{
var modelStateValue = GetModelStateValue<string>(Html, fieldName);
item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase);
}
}
}
the error happens on item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase);
The wizard code for the “back” button on the wizard looks like this in the controller:
public ActionResult EMailQuoteConfirm(string backButton, string nextButton)
{
if (backButton != null)
return RedirectToAction("EMailQuoteBasicDetails");
else if (nextButton != null)
return RedirectToAction("EMailQuoteSubmitted");
else
return View("EMailQuote/Confirm", myData);
}
Any advice is appreciated.
After much research into the issue I determined it was serialization that was causing this weird error. And although serialization worked well, I’ve opted for a jQuery solution which works better with this code.
Lesson learned: jQuery ain’t so bad. 🙂