Domanda

I'm used to EF and am in a dev shop that doesn't use it. My model is as such:

@model List<ControlNumberViewer.Models.Table>

I need to access the model to fill a Html.DropDownList(). I just don't know what to use to access the model...

È stato utile?

Soluzione 2

You need to use the Model keyword. For example:

@Html.DropDownList("elementName", Model.Select(x => new SelectListItem
{
    Text = x.Id, // some value from your `Table entity
    Value = x.Name // // some value from your `Table entity
});

Altri suggerimenti

Create a view model and populate it with the contents of your table in the controller (or some other abstract layer). Only pass your view model to your view.

In any project besides the most basic ones, you don't want to pass a business model directly to the view, whether using Entities, ADO.NET or any other technology, and this is a good reason why. Your view should not have to be changed just because your data access layer changes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top