I was just wondering, if by moving complex if else statements and the resulting html markup to the code behind violates some ‘MVC’ law?
It seems like a great option when faced with inline if else statements that can become extremely unreadable.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I prefer not to use the code behind class in my views. This is not because it violates MVC by default, but because I found that the ‘natural’ way (at least for me) is different.
When I face complex HTML markup that relates to purely view concerns, I usually write an extension method for
HtmlHelperclass in order to hide the complexity. Thus I have extensions likeHtml.MoneyTextBox(),Html.OptionGroup()andHtml.Pager<T>.In other cases when complex conditions arise, usually I missed something from the controller. For example, all issues related to the visibility, readonly or enabled of elements usually stem from something that the controller can provide. In that case instead of passing the model to the view, I create a view model which encapsulates the model and the additional info that the controller can provide in order to simplify the HTML markup. A typical example of view model is the following:
That said, the code behind is part of the view, so you can use it freely, if it fits your needs better.