Your models retrieve data, your facilitator organizes the data for presentation, and presentation controls associate your model with user interface elements. For example, here is the model for LogEvent:
public class LogEvent{ public string Title {get;set;} public string Date {get;set;} public string Message {get;set;}
Here is the controller that processes the user request to view all the logs:
[DependencyPropertyLolJk] protected ILogProvider MyLogProvider {get;set;} // set by DI [AcceptVerbs(HttpVerbs.Get)] public ActionResult Logs() { return View("Logs", LogEvent.GetAllLogs(MyLogProvider).OrderByDescending(x => x.Date)); }
And here is the view and how it is attached to the model:
<table> <thead> <tr> <th> Date </th> <th> Title </th> <th> Message </th> </tr> </thead> <% foreach(var log in ViewData.Model) %> <tr> <td><%= log.Date %></td> <td><%= log.Title %></td> <td><%= log.Message %></td> </tr> <% }; %> </table>
So, you see, you have to write your html using inline code. This works well for simple user interfaces, but can be complicated and hardworking when it comes to more complex things like pagers and gridviews.
When your user interface gets complicated, the easiest way is to create extensions to the HtmlHelper class. Here are two examples that show how this can reduce the complexity of your interface: HtmlHelper GridView and Pager . I created similar helper methods, and it is pretty wonderful how you can mix html and inline code in lambdas. Now, if the developer could only format this kind of mixed code / markup decently ...
Will
source share