0

I have about 4 tables from which I like to gather information and then show it in a view. From my understanding, I would need to create a class in Model. In the Controller, I would then call that class. I would then pass an instance of that class to the view.

I am not sure though on how to do this programatically. If some code can be provided as a simple example. Also I may need to edit and delete the records I show on the view as well.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • It will be easier for us to know which tables (show the names and columns) and details in what you want to render in your view. – Polity Oct 21 '11 at 13:45
  • @Polity: Why is that necessary? – jgauffin Oct 21 '11 at 13:47
  • @jgauffin - I said easier, not necessary. Based on his question, this person is not very familiar with ViewModels. I would assume it would be easier for him if we can give him an example within his context – Polity Oct 21 '11 at 13:49

2 Answers2

1

Do you already have the Data Access Layer up and running or how do you plan to read the data from the database and get it to the MVC application?

Are you planning to use SQL Server and Entity Framework or what?

For general ideas on how to layer an ASP.NET MVC application see my answer here: MVC3 and Entity Framework in fact that answer applies to general architecture not only MVC but there are some comments below about how to split and keep separated various concerns.

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Hello, we are using .edmx files. I have added the tables I need to the .edmx file. I started creating the Controller but in one of the options in the drop down is Model Class. I do see the ables I added to the drop down but not sure which one to choose as they are all different tables. Not sure how to choose a consolidated model for my case. – Nate Pet Oct 21 '11 at 15:02
  • see my linked answer above. You would first of all need to create a BL and a DAL so that your UI (MVC application) does not talk with the edmx directly. This is a favorite or suggested approach of course, and it pays off by keeping UI only doing UI and not dependent on DAL and EF. – Davide Piras Oct 21 '11 at 15:22
1

Most of us are using a ORM like nhibernate or entity framework to handle data access. It makes everything strongly typed (if the mappings work OK ;)) and speeds up the development time.

What you should do is to fetch your tables with a data access layer and use those to fill a view model with the information. Something like:

public ActionResult Details(int id)
{
    var user = _nhibernate.Get<User>(id);
    var company = _nhibernate.Get<Company>(user.CompanyId);
    var model = new DetailsModel{
       CompanyName = company.Name,
       UserName = user.Name
    };
    return View(model);
}

And the DetailsModel should be created in the WebProject\Models folder. I usually create a subfolder for each controller.

WebProject\Models\ControllerName

public class DetailsModel
{
    public string CompanyName { get; set; }
    public string UserName { get; set; }
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • I a question on creating the controller in my scenerio where I have a number of tables. By the way, we do use .edmx. To create the model, what I did was go to the Model folder and created a model with the following: `public class ModelVehicles { public IEnumerable Trucktbl { get; set; } public IEnumerable Cartbl { get; set; } }` where Trucktbl and Cartbl are the name of the tables. I do get a swiggly line under IEnumerable though and it says the name IsEnumerable could not be found.. My first goal is to get the model in place for the creating the controller – Nate Pet Oct 21 '11 at 14:02
  • My example fetches information from two different tables and add that information to the view model which is then used by the view. There is nothing that says that you must fetch information from all four tables with a single SELECT statement. – jgauffin Oct 21 '11 at 14:03
  • In the model DetailsModel, where are CompanyName and UserName the names of tables? If not, how do you specify tables in the Model class? – Nate Pet Oct 21 '11 at 14:11
  • i don't understand what you are trying to do? `_nhibernate.Get(user.CompanyId)` get info from the company table and `_nhibernate.Get(id);` get information from the user table. Entity Framework (edmx) works in a similar way – jgauffin Oct 21 '11 at 14:20
  • Thank you for your response. What did was add the tables to my .edmx file. Next I started creating a controller. For the scaffolding option, I selected Controller with read/write actions. For the Model class, I do see the individual tables that I added in the drop down but which one do I choose? – Nate Pet Oct 21 '11 at 14:42