0

I've noticed that one of my ASP.NET MVC apps started generating a NullReferenceException error when a custom error message is added to the model state. When that happens, I end up getting an error on one of the Html.TextBoxFor calls the view uses. The controller looks like this:

public async Task<ActionResult> ConfirmPayment(PaymentInfo model){
  try{
    //this will generate an exception
    PaymentManager.Pay(...);
  }
  catch(PaymentException ex){
   _logger.Error(ex);
   var field = MapErrorToField(ex); //this returns the name of one of the fields
   ModelState.AddModelError(field, "error");
  }

I've found some links that suggested the problem could be solved by calling SetModelValue. Unfortunately, that did not solve my problem.

SOLVED IT

The problem: the view had some code that looked like this:

<div class="form-group @(!ViewData.ModelState.IsValid && ViewData.ModelState[nameof(Model.ReturnTicket)].Errors.Count > 0) ? "has-error" : "")">
...

Now, the thing is ReturnTicket won't be present in all the requests, so there will be times when there's no Model.ReturnTicket in the ModelState dictionary. The solution is simple:

<div class="form-group @((!ViewData.ModelState.IsValid&& ViewData.ModelState.Keys.Contains(nameof(Model.ReturnTicket)) && ViewData.ModelState[nameof(Model.ReturnTicket)].Errors.Count > 0) ? "has-error" : "")">
....

The additional condition solves the error. Unfortunately, the error message pointed to a completely different place (like 10 lines above of where thr problem was...), making it really hard to understand what's going on...

Thanks again.

Luis Abreu
  • 4,008
  • 9
  • 34
  • 63
  • 3
    `ModelState.AddModelError` has got nothing to do with with your error. Something is `null` when you pass the model back to the view (assuming you even return a model - you have not shown any of the relevant code) –  Feb 02 '17 at 10:20
  • Unfortunately, I think you're wrong. If I commend the Modelstate.addModelError call, there's no error and the view gets rendered correctly – Luis Abreu Feb 02 '17 at 11:06
  • No I'm not. That cannot generate that error, but what can is when you then return the view back to the model (using `return View(model);`) and something that you accessing in the model is `null`, or perhaps you did not even return the model, but since you have not shown the relevant code, no one can help, so read the dupe and learn to debug your code. –  Feb 02 '17 at 11:09
  • `System.ArgumentNullException` will be thrown if the `field` is null and when you try to add it to model state - `ModelState.AddModelError(field, "error")` . @LuisAbreu - Could you confirm the value of `field` ? – Developer Feb 02 '17 at 11:43
  • @Developer, That would throw a different error - _Value cannot be null.Parameter name: key_ –  Feb 02 '17 at 11:45
  • @StephenMuecke - The exact error is - _An exception of type 'System.ArgumentNullException' occurred in System.Web.Mvc.dll but was not handled in user code_ _Additional information: Value cannot be null._ And message would be - _Value cannot be null. Parameter name: key_..Just want to confirm with OP as he says _If I comment the Modelstate.addModelError call, there's no error and the view gets rendered correctly_... – Developer Feb 02 '17 at 11:48
  • @Developer - we may be using different versions of MVC, but its irrelevant - OP has stated _I end up getting an error on one of the Html.TextBoxFor calls the view uses_ –  Feb 02 '17 at 11:52
  • @StephenMuecke - yeah, I agree.. question is real vague – Developer Feb 02 '17 at 11:57

0 Answers0