0

At first i have to say, i wanted to ask this question on programmers but i don't have enough reputation in this stackexchange.

While i was developing a ASP.Net MVC (4) Application i came to an issue where i had to display data from a model in a view where i need a recursive function which builds the HTML for me.

I asked myself where such a funtionality should be placed in best practices. The model should not hold any logic and the controller does not communicate with the view in common. And i really don't want to put complex logic into my view.

This is a theoretically question and i hope it is ok that i asked in this forum without giving source code.

3
  • Create a htmlhelper extension method that generates you html - e.g. this answer Commented Aug 24, 2015 at 6:11
  • Yeah that idea came to my mind too, but call me patient but i try to dodge every static class in ASP.Net which isn't really a must have. (I don't trust the app pool management when it comes to static classes) Commented Aug 24, 2015 at 6:27
  • Ever single htmlhelper in MVC is a static class. MVC would not be very effective without them. There is nothing to be concerned about Commented Aug 24, 2015 at 6:29

1 Answer 1

1

You are quite right to keep your concerns separate. What you're looking for is a ViewModel.

What is ViewModel in MVC?

Sign up to request clarification or add additional context in comments.

3 Comments

I read the answers but it is still not clear where to put HTML building or in general HTML helper methods in MVC for me. Anyway i would love a soultion where i don't have to change my concept. Thanks
As suggested by Stephen Muecke you can achieve what you want without changing your concept by creating your own html helper (Custom Html helpers).
The ViewModel would essentially be a representation of your Model that only contains properties you want to display in a view, as well as anything else that doesn't exist in the model. A simple example is a model that contains strings for FirstName and LastName, but in your view you want to always show the full name. Your ViewModel could have member variables for FirstName and LastName, and then this: public string FullName { get { return FirstName + " " + LastName } };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.