Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

There's probably a really basic answer to this question but I am new to Entity and MVC and am getting used to the basics.

I'm trying to automatically generate a MVC controller for the main table Sites with a dropdown for server. It seems like I would need a model like this:

   public class Sites
    {
    public TTSites TTSites { get; set; }
    public List<servers> server { get; set; }

    public Sites()
    {
        server = new List<servers>();
    }
   }

This is using the classes TTSites and servers both with string server But if I set this as my model class and my entity database as data context it says I need to define a key. Should I be using the base classes instead of the model or what? Do i need to set something up in the model or base class?

share|improve this question

1 Answer 1

It seems like you've got some terminology confused. You code the controller actions in a controller class, and the routing engine determines what controller action to call based on the URL. For example, if you have a HomeController class with a default Index action, it might look like this:

public ActionResult Index()
{
    // code here
}

This would be invoked with the default routing, if you went to your site with a URL like this (let's say your site can be hit via the www.mysite.com URL:

http://www.mysite.com/Home

That would get you into the Index action in the controller.

Ordinarily, one would use a view model to use on the UI side, and that would be populated from an entiy with the data you need in the view itself. If you had two entities like TTSite and Server, you'd populate the Sites view model like so, as a (very simple) example:

public ActionResult Index()
{
    var servers = yourDbContext.Servers.ToList();
    var ttSite = yourDbContext.TTSites.GetByID(1); // retrieve one entity by its ID value, this would be acquired dynamically based on some sort of user input rather than hard-coded
    var viewModel = new Sites(servers);
    viewModel.TTSite = ttSite;

    return View(viewModel);
}

I'm not including anything regarding making drop-downs, just illustrating getting data into a view model and then creating a view with that view model.

Note that you would not use the Sites class as an entity but rather a view model, and setting its data based on entities from your database. You wouldn't be setting any primary keys in a view model class; those are the concern of the data model, and you've presumably already got those entities (such as TTSite) set up in a usable fashion in your data layer.

Once you've got a controller action and a view up and working, you can turn to getting the view model data into a form usable by a drop-down list, and going from there.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.