ASP.Net MVC framework and data binding

I am having some problems understanding some concepts of the MVC framework. I am making a very simple application that classifies products.

The Creation screen will simply use the drop-down list showing the list of categories, product name and send.

In a regular .Net application, I would bind a remote list of servers in Page_Load, but in an MVC application, what is the best way to get my categories from the database and add them to the drop-down list?

(Sorry, my question is extremely nubish, but, unfortunately, resources are dumped on MVC, and examples are often interrupted due to early changes)

+3
asp.net-mvc
source share
2 answers

I'm not sure I fully understand, but if your page displays a single product, and the only user input is just to select a category from the drop-down list, I can sort the help (but I also!).

Link to this page:

http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-preview-3-release.aspx

You will want to create a SelectList in your controller for categories (possibly referring to an identifier and displaying a name). Then you add this SelectList to the ViewData. You could alternatively set this as part of your ViewData.Model and reference it from your view.

In your view, you are using the HtmlHelper for the DropDownList, which selects the SelectList as the parameter.

This link above should convey it much better, this is just a short summary. This link is for preview 3, but I think it should all be applicable.

+4
source share

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;} // this is for example only; you would most likely bind directly against the host.GetAllLogs() result public static IEnumerable<LogEvent> RetrieveAllLogs(ILogProvider host){ return from x in host.GetAllLogs() select new LogEvent(x.LogTitle, x.Date, x.Message); } 

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:

 <!-- header left out for brevity --> <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 ...

0
source share

All Articles