views:

1862

answers:

2

Hi,

I am having some trouble grasping some concepts behind the MVC framework. I am doing a very simple application which categorizes products.

The Creation screen will simply use a dropdown list showing the list of categories, the name of the product and submit.

On a normal .Net app, I would databind a server dropdownlist in the Page_Load, but in a MVC app, what is the best way to retrieve my categories from the database and add them to the dropdown list?

(Sorry, my question is extremely noobish but unfortunately resources are spare on MVC, and examples are often broken due to early changes)

A: 

Your models retrieve data, your presenter organizes data for the view, and the view controls binding your model to UI elements. For example, here's a model for a 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's the controller that handles a user request to view all 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 binds 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 UIs, but can be complex and a drudgery when it comes to more complex things, such as pagers and gridviews.

When your UI gets complex, the easiest thing to do is to create extensions to the HtmlHelper class. Here are two examples that show how this can cut down the complexity of your UI: HtmlHelper GridView and Pager controls. I've created similar helper methods, and its pretty remarkable how you can mix html and inline code within lambdas. Now if the designer was only able to format this kind of mixed code/markup decently...

Will
sorry, I only meant the Databinding part (I know how MVC is supposed to work, I'm stuck on the implementation details :))
Luk
+4  A: 

I'm not sure if I totally grasp, but if your page is displaying a single product and the only user input is just to select a category from a drop down list, I can sorta help (but I'm a noob too!).

Referencing this page:

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

You are going to want to create a SelectList in your controller for Categories (probably referencing an ID, and displaying a Name). You then add this SelectList to your ViewData. You could alternatively set this as part of your ViewData.Model and reference that from your View.

In your view you use the HtmlHelper for DropDownList that takes a SelectList as a parameter.

That link above should convey it much better, this is just a quick summary. That link is for preview 3, but I think it should all still apply.

eyston