MVC3 Views: Working with Zero Models with Grace

Too often, I come across a situation where a view in my project throws a null reference reference exception.

@model Johnny.Application.TestModel <div>@(Model.SomeText)</div> 

This causes an error if the Model is null .

But how do people deal with this? Of course, I don't see code examples around the world with ugly null checks clogging up code in the view. This makes me think that most of the time, controllers should not return null models. But how can you enhance this with more sophistication?

Now, as soon as someone accidentally forces the controller to return a null model, the view model explodes and looks guilty. In reality, it was a controller error. And the view may not even β€œcatch” the problem, it will only happen if the model members get used to it (which, in most cases, of course).

For various reasons, some views may handle null values. I would not expect this to be the main case. Clearly, this is a matter of setting some kind of β€œcontract” between the view and the controller.

I don't like the options I saw:

  • Check if the model is zero every time it is used. Very lame!
  • One big if statement that wraps the whole view with a null check model. Think of an incomplete property code. Lame!
  • Add if you check with a throw at the top. Not bad, but it seems stupid. Softly limping.

I would like to know if there is something like these options for setting a "no nulls" contract:

  • Controller method attribute, for example [NoNullModels]. I doubt that this exists, because I do not think the controller knows what kind it is connecting to.
  • In the view, an indicator such as @ MVC3.HeyDontAllowNulls or some other standard way of throwing an exception (for example, option 3 above)
+6
source share
3 answers

I asked a similar question here Should I try to protect exceptions from exception exceptions / exceptions in an exception in MVC views? and get good answers to it. In short, it is preferable to add null checks in controllers and, possibly, even unit tests, and not in your representations.

+2
source

There are many settings here, some of which you can do:

  • Throw RecordNotFoundException in your data layer (custom exception) and global exception filter to catch this
  • Check if the returned data is returned from your repository and is processed in each case.
  • Decorate your controllers to look for a null model in GET methods that are not CREATE methods (or any rules that you think are OK) with an action filter attribute to check this in OnActionExecuted

Even my "CREATE" views usually get a model, even if they are empty (although they quite often have IEnumerable <SelectListItem> in them), so they should always have a model for them.

0
source

I check zeros in my ideas sometimes in those parts that require it.

I also sometimes create default values ​​in my controller, if there is a zero value, it depends on what you are trying to do and what is acceptable.

For example, I have a case when people subscribe to something and set notifications. If they do not have notifications, the sub-object of my model is NULL. I do not want to set default values, so I have a check. In other sections, I use only the default value.

0
source

Source: https://habr.com/ru/post/909750/


All Articles