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.

DavidVollmers
  • 677
  • 5
  • 17
  • Create a htmlhelper extension method that generates you html - e.g. [this answer](http://stackoverflow.com/questions/27146524/how-to-render-singly-linked-list-in-mvc-view-page/27147744#27147744) –  Aug 24 '15 at 06: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) – DavidVollmers Aug 24 '15 at 06: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 –  Aug 24 '15 at 06:29

1 Answers1

1

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

What is ViewModel in MVC?

Community
  • 1
  • 1
Eraph
  • 1,019
  • 1
  • 10
  • 21
  • 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 – DavidVollmers Aug 24 '15 at 06:09
  • As suggested by Stephen Muecke you can achieve what you want without changing your concept by creating your own html helper (Custom Html helpers). – Ramesh Babu Aug 24 '15 at 06:21
  • 1
    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 } };` – Eraph Aug 24 '15 at 22:19