views:

39

answers:

1

Hi,

I'm new to MVC and the new paradigm hasn't clicked yet. I want to create a user control that is a simple dropdown and uses data from a database to populate its items. The dropdown would be useable in any view I chose. At this point I don't need the control to be data/table agnostic, I'd settle for making the user control strongly typed like a view.

The scenario I'm looking at currently is to use the SelectedIndexChanged event (or its equivalent in MVC 2) to populate a table of related data.

Here is the markup from the Home\Index.aspx page for the html helper I'm trying to use.

<%: Html.DropDownList("SalesTerritories", new SelectList(MvcAdventure.Models.SalesTerritory, "TerritoryID", "Name")) %>

The dropdown helper doesn't work because 'MvcAdventure.Models.SalesTerritory' is invalid in this context (as the runtime told me when it went to render the page).

thanks,

Mike

A: 

I believe what you are looking for is a Templated Helper.

It lets you define a bit of markup that you can reuse all over your application, like so:

<td>
    <%= Html.DisplayFor(Product=> item.SellStartDate, "Date") %> 
</td>
Nate Bross
Thanks, I had gotten this far too. The problem I'm having is tying the data into the templated helper. I have an entity model that contains the data I want to use. In a strongly typed view I'd refer to Model.... to get at the data. But since this view isn't strongly typed and I want to display data from multiple tables on it I can't strongly type the view. If I wanted to use AdventureWorks.SalesTerritories for the dropdown how do I access the AdventureWorksEntities context to pull the SalesTerritories TerritoryID and Name fields into the dropdown?
flyfisher1952
Here is where I'm at on this. In my HomeController.cs Index methodvar territories = from st in entities.SalesTerritories select st;ViewData["SalesTerritories"] = new SelectList(entities.SalesTerritories.ToList(), "TerritoryID", "Name");In Home\Index.aspx<%: Html.Partial("SalesTerritoryDropDownViewUserControl")%>In Views\Shared\SalesTerritoryDropDownViewUserControl.ascx<%: Html.DropDownList("SalesTerritories")%>The error I get is: error CS0234: The type or namespace name 'SalesTerritories' does not exist in the namespace 'MvcAdventure.Models'In
flyfisher1952