steven
steven

Reputation: 393

Best practice rendering model attributes: where check whether they are null or string empty

I have a model with several attributes like name, telephone, email, adress et cetera and sometimes some attributes are empty.

as I understand the best practice of mvc I should NOT put HTML in my model like e.g.

if (string.isNullorEmpty(_username))
    username = "<span>" + _username + </span>;
else
    username = string.Empty;

so I am asking myself, where should i check, if the attributes are empty or not. I think it is also not recommended to do this in the view like:

<% if (Model.username != string.empty) { %>
    <span><%: Model.username %></span>
<% } %>

What is the best practice for checking model attributes whether they are empty or not???

thanks for your opinions. I am excited to hear from you!!!

Upvotes: 0

Views: 302

Answers (1)

markpsmith
markpsmith

Reputation: 4918

I don't think there's anything wrong with checking for null values in the view, I suppose the alternative would be to create a viewmodel for every eventuality and do the checking in the controller.

Upvotes: 0

Related Questions