Thursday, March 31, 2011

ASP.NET MVC : Null property values in model ?

Considering the following view excerpt :

<%= Html.TextBox("Something", Model.Property.SubProperty.Value) %>

I can handle the case when Property and SubProperty are null with :

<% if( Model.Property != null || Model.Property.SubProperty != null ) { %>
<%= Html.TextBox("Something", Model.Property.SubProperty.Value) %>
<% } %>

But I don't find this very clean. Anyone got an idea ?

I'm thinking about automapper. Good idea ?

From stackoverflow
  • Perhaps you shoudl consider having your controller provide an object to the view that is more suitable to its needs.

    mathieu : You mean "flattened" ?
    AnthonyWJones : Yes flattening might help or alternatively ensuring the Property would not be null. Sometimes its better for the View to simply get a class created by the controller rather than hand the view an object from the Model.
    mathieu : Ok, I'll try that with AutoMapper

0 comments:

Post a Comment