Playing around with viewmodels to try and get my head around it, as we came to learn about half way through a project that using Entity Framework models to display the data isn't the way we should be doing it, so we're trying to learn the right way, or I think we are anyway.
I have a table called Users in a database, and have created an ADO.net Entity Data Model which I simply called "MainModel" for now, and then created a new Class file called "MainViewModel".
In this view model, I've taken properties from the User table in MainModel which we would want to display in a view on a table to show every database entry. The MainViewModel code is as follows:
namespace WebApplication9.Models
{
public class MainViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Company { get; set; }
}
}
I then have a controller called MainController, which has an ActionResult for Index which is as follows:
public ActionResult Index(MainViewModel viewModel)
{
var users = new User()
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
EMail = viewModel.Email,
Company = viewModel.Company,
};
List<MainViewModel> viewModelList = new List<MainViewModel>();
return View(viewModelList);
}
Up to this point, I have no idea where to go next and unfortunately everything I've read is just confusing me more. It does display a table, with the FirstName, LastName, Email and Company table headers, but it doesn't display any data in the view.
Any help would be hugely appreciated as we're desperate to get going again :)
Thanks