0

I have two database classes as defined below:

 public class TopDate
    {
        [Key]
        public int DateId { get; set; }
        public DateTime Date { get; set; }
    }
 public class TopSong
    {
        [Key]
        public int SongId { get; set; }        
        public string Title { get; set; }
        public int DateId { get; set; }
    }

where DateId is foreign key to TopSong

I am creating a controller through which i can create, delete or edit these database values.

When i right click on controller class and add controller i can only select one of the two classes defined above. Is there a way to make 1 controller to handle database updates to both these tables on one page?

Error Image: enter image description here

NoviceMe
  • 3,126
  • 11
  • 57
  • 117
  • You can have one controller to handle both classes but while creating the controller don't select a class. – VJAI Aug 29 '12 at 03:18
  • @Mark It gives an error if i do not select a class while creating the controller - The model type is invalid. Please select an item from the list. – NoviceMe Aug 29 '12 at 03:25
  • @Mark - Posted the error i am getting as an image. – NoviceMe Aug 29 '12 at 03:29
  • 1
    The template you have selected *Controller with read/write actions..* needs a model. You can use the default template but in that case you have to add the methods manually. – VJAI Aug 29 '12 at 03:34
  • I suggest you to have a look at @Tommy answer – VJAI Aug 29 '12 at 03:35

2 Answers2

1

Your controller should not be dealing directly with domain objects (meaning those things that are directly associated with your database). Create a ViewModel that contains the properties that you need, use your service layer to populate the ViewModel and your controller will use that as the Model for its base. An example of your ViewModel could be something like the following given your description above:

public class MusicViewModel
{
    public int SongId {get;set;}
    public string Title {get;set;}
    public IEnumerable<DateTime> TopDates {get;set;}
}

This view model would contain a list of all dates that a specific song was a Top Song.

Tommy
  • 39,592
  • 10
  • 90
  • 121
  • It gives an error: No key is defined even though i am using like this: [Key] public int SongId {get;set;} – NoviceMe Aug 29 '12 at 03:43
  • You do not need a 'key' as this is not a database object, it is a view model that contains the relevant data for the view. You use a service layer to query your database objects and populate the view model. That view model is what the controller will send/recieve from the views for creating, updating, displaying, etc. – Tommy Aug 29 '12 at 03:50
  • Here is a decent SO answer on some more detail of what you need to accomplish your task...http://stackoverflow.com/questions/7735635/real-example-of-tryupdatemodel-asp-net-mvc-3 – Tommy Aug 29 '12 at 03:54
  • do i need a using or something up top? I still dont get why i am getting error? – NoviceMe Aug 29 '12 at 03:54
  • It may benefit you to look at some of the MVC tutorials around Unit of Work, View Models and such. http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3 – Tommy Aug 29 '12 at 03:55
  • i looked at the tutorial. But if i select one class it creates the controller. But if i add both class methods together that is when i am getting error which is throwing me off. – NoviceMe Aug 29 '12 at 04:04
  • Here is the error- System.Data.Entity.Edm.EdmEntityType: Entity Type 'MusicViewModel' has no key defined. Define the key for this Entity Type. – NoviceMe Aug 29 '12 at 04:07
0

The objects you showing (code) are database classes (so called domain objects). What you need to do is to define a view model, a standard ASP MVC practice: you define a class, that is tailored for specific view and only containing data relevant to that particular view. So you will have a view model for a view that will create a song, another that will update it etc.

Actually situation you describing is classical situation to use view models. Using domain objects in the views, however, is really really bad practice and prone to more problems than you want to deal with.

Hope this helps.

Display Name
  • 4,672
  • 1
  • 33
  • 43