Changing from using Model to ViewModel - asp.net-mvc

I have an index page that lists all users. This has the following in the Users controller -
public ViewResult Index()
{
return View(userRepository.AllIncluding(user => user.Roles));
}
and then the view starts with
#model IEnumerable<TRS.Models.User>
and then uses
#foreach (var item in Model) {
to loop through all users in my model.
I now need to change my model to a ViewModel that contains both the User model and an extended UserDetails model.
I have changed my Index view to use the view model -
#model IEnumerable<TRS.ViewModels.RegisterViewModel>
But I don't know how I should be going about filling the ViewModel in my controller -
public ViewResult Index()
{
var viewModel = new RegisterViewModel
{
UserName = "???"
FirstName = "???"
LastName = "???"
};
return View(viewModel);
}
I assume I need to create an instance of the view model and then pass it to the view. But I don't understand how I can get data against each individual item. I'll obviously need to get all data for all users here and then loop through them in my view. Any ideas what I should be going in the controller above? What should replace the "???" with that will fill the viewModel with all the data? Or is this the wrong approach?
Thanks
Edit - Models added -
public class User
{
[Key]
public string UserName { get; set; }
public virtual ICollection<Role> Roles { get; set; }
public virtual UserDetails UserDetails { get; set; }
}
public class UserDetails
{
[Key]
public string UserName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
[Required]
public virtual User User { get; set; }
}
Edit - From View -
<td>
#foreach (Role role in item.Roles){
#role.RoleName <br />
}</td>

public ActionResult Index()
{
var usersVm = userRepository
.AllIncluding(user => user.Roles)
.Select(user => new RegisterViewModel
{
UserName = user.UserName
FirstName = user.UserDetails.FirstName
LastName = user.UserDetails.LastName
});
return View(usersVm);
}

Automapper was created for this very thing. I'd highly recommend it.
Take a read of getting started section of wiki.
Once you have it configured your controller code would look something like this:
List<RegisterViewModel> registrants =
Mapper.Map<List<User>, List<RegisterViewModel>>(users);
Though you might want to consider a viewmodel that has a list of registrants on them but that is up to you.

Your viewModel
Public Class RegisterViewModel
{
Public IEnumerable<TRS.Models.User> AllUsers {get;set;}
Public IEnumerable<TRS.Models.UserDetails> UserDetails {get;set;}
}
Then in your controller
public ViewResult Index()
{
var viewModel = new RegisterViewModel
{
AllUsers = userRepository.AllIncluding(user => user.Roles).ToList()
};
var DetailList = new list<TRS.Models.UserDetails>();
foreach(var user in viewModel.AllUsers)
{
DetailList.add(new userDetails
{
name = user.Name,
age = user.age,
....
}
}
viewModel.UserDetails = DetailList;
return View(viewModel);
}

it is unclear from your question what UserDetails look like.
You should not use IEnumerable<RegisterViewModel> in your view. The RegisterViewModel should be the container of everything you will need in the view:
public ViewResult Index()
{
var usersDetails = GetUserDetails(userRepository.AllIncluding(user => user.Roles));
var viewModel = new RegisterViewModel
{
UserDetails = usersDetails.ToList(),
SomeMoreDataRequiredByTheView= "1"
};
return View(viewModel);
}
//you can use automapper to do this dirty job.
private IEnumerable<UserDetails> GetUserDetails(IEnumerable<User> users)
{
foreach(var user in users)
{
yield return new UserDetails()
{
FullName = user.FirstName + " " + user.LastName,
// Other stuff you want
};
}
}

Related

How to display a list in View in MVC?

Ok, so I have seen a lot of similar questions, but unfortunalety I can't figure out how to access a list of users from db in the View. I get a System.NullReferenceException. Can one of you experts see what I am doing wrong?
Model
public class MyModel
{
[Key]
public int Id { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
public bool TriggerOnLoad { get; set; }
public string TriggerOnLoadMessage { get; set; }
public string EmployeeNumber { get; set; }
public IEnumerable<User> Users { get; set; }
public List<MyModel> GetAllUsers()
{
var queryString = "SELECT Name FROM Users";
var adapter = new SqlDataAdapter(queryString, System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString);
var current = new DataSet();
adapter.Fill(current, "Name");
return (from DataRow item in current.Tables[0].Rows
select new MyModel()
{
Name = Convert.ToString(item["Name"]),
}).ToList();
}
Controller
public ActionResult GetUser()
{
var model = new MyModel();
_db.GetAllUsers();
return View(model);
}
View
#model ModelName.Models.MyModel
--HTMLCode--
#foreach (var item in Model.Users) <-- Exception.
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Name)</td>
</tr>
}
What am I forgetting?
Your problem is that you are getting the users, but you are not passing the users to the view, instead, you pass an empty model.
A better approach would be to create a ViewModel with all the properties you need in the view.
Let's say your view model looks something like this:
ViewModel:
class YourViewModel
{
public string Name { get; set; }
public bool TriggerOnLoad { get; set; }
public IEnumerable<User> Users { get; set; }
}
After creating your ViewModel what you need to do is that you need to create an instance of the ViewModel in your Controller and fill it with data and then pass the ViewModel to the View. In your case it would look something like this:
Action:
public ActionResult RandomAction()
{
YourViewModel vm = new YourViewModel()
{
Users = _db.GetAllUsers(),
Name = "Random Name",
TriggerOnLoad = true
};
return View(vm);
}
Later on, if you decide you need some extra properties you need to work with in your view, you just add them to your ViewModel and continue on.
Pass users like this. You are not passing the users to View
public ActionResult GetUser()
{
var model = new MyModel();
var users = _db.GetAllUsers();
return View(users);
}
Then in View
#model List<MyModel>
#foreach (var item in Model) //foreach through the list
{
<tr>
<td>#Html.DisplayFor(model => item.Name)</td>
</tr>
}

Querying database but getting null back

I'm trying to list the items from my database into my view but I'm getting null back.
I know the connection must be working to a certain extent because in my database the tables didn't exist but once I ran my program it did create the tables. However when I add content into my table my view still returns NULL.
Also, haven't touched the Review table yet, just worried about getting Restaurants working.
Restaurant.cs
namespace OdeToFood.Models
{
public class Restaurant
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
public ICollection<RestaurantReview> Reviews { get; set; }
}
}
OdeToFood.cs
namespace OdeToFood.Models
{
public class OdeToFoodDb : DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<RestaurantReview> Reviews { get; set; }
}
}
Controller
OdeToFoodDb _db = new OdeToFoodDb();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View();
}
Index.cshtml
#model IEnumerable<OdeToFood.Models.Restaurant>
#{
ViewBag.Title = "Home Page";
}
#{
if (Model != null)
{
foreach (var item in Model)
{
<div>
<h4>#item.Name</h4>
<div>#item.City, #item.Country</div>
<hr />
</div>
}
}
else
{
<h1>Null</h1>
}
}
You need to pass to model back to the view.
OdeToFoodDb _db = new OdeToFoodDb();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View(model);
}
You never actually send the model to the view. Pass it as an argument:
OdeToFoodDb _db = new OdeToFoodDb();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View(model);
}
Additionally, it's generally a good idea not to create database contexts in a shared scope. Keep the context as close to where it's used as possible and only expand its scope when you really need to. Something like this:
public ActionResult Index()
{
using (var _db = new OdeToFoodDb())
{
var model = _db.Restaurants.ToList();
return View(model);
}
}
Database contexts/connections in a shared scope is just asking for problems unless you pay close attention to what you're doing. As the code gets more complex, it becomes more likely that other methods will try to use it and it may be in an unknown state at that time.

MVC-Viewmodel and LINQ, Insert value into a table and the value gets tagged by two other values in two other tables

I am using MVC-Viewmodel with repository pattern with EF on my project.
I have 3 tables, Question, CoreValue, SubjectType.
SubjectType and CoreValue are many to many associated with Question and these two tables are not suppose to get any new values, but users can create questions so Question table will get new data when a user creates it. I use two dropdownlists for CoreValue and SubjectType so that the user can choose a CoreValue and a SubjectType when they create a Question.
Here is my HTTPGET controller action:
// GET: /Admin/Create
public ActionResult Create()
{
CoreValueRepository Crep = new CoreValueRepository();
SubjectTypeRepository Srep = new SubjectTypeRepository();
CreateViewModel model = new CreateViewModel();
List<SubjectType> subjectypes = Srep.getall();
List<CoreValue> corevalues = Crep.getall();
model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
model.CoreValues = new SelectList(corevalues, "CID", "Cname");
return View(model);
}
And here is my Viewmodel:
public class CreateViewModel
{
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname { get; set; }
public SelectList SubjectTypes { get; set; }
public SelectList CoreValues { get; set; }
}
I use Repository for CRUD operations and viewmodels for handling data in UI.
Now I have to code the HTTPPOST Action Create in my controller for inserting Question data to my database, and the questions need to be tagged with CoreValue ID and SubjectType ID. So I was thinkin about to start coding the HTTPOST action Create, and I was wondering if someone could help me out with this.
Thanks in advance!
Best Regards!
This is how i would handle it :
In your ViewModel, replace :
public class CreateViewModel {
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname { get; set; }
public int SubjectTypesID { get; set; }
public int CoreValuesID { get; set; }
}
In your HTTPGET put your list in Viewbags :
public ActionResult Create()
{
CoreValueRepository Crep = new CoreValueRepository();
SubjectTypeRepository Srep = new SubjectTypeRepository();
CreateViewModel model = new CreateViewModel();
ViewBag.SubjectTypes = Srep.getall();
ViewBag.CoreValues = Crep.getall();
return View(model);
}
To use the viewbag in your view you can use this :
#Html.DropDownList("SubjectTypesID ", new SelectList(ViewBag.SubjectTypes as System.Collections.IEnumerable, "SID", "Sname", Model.SubjectTypesID ))
#Html.DropDownList("CoreValuesID ", new SelectList(ViewBag.CoreValues as System.Collections.IEnumerable, "CID", "Cname", Model.CoreValuesID ))
Your HTTPOST :
[HTTPOST]
public ActionResult Create(CreateViewModel model)
{
//Now with your model you have the Id of CoreValue and SubjectType
//You could do
if (ModelState.IsValid)
{
QuestionRep.Add(model);
return RedirectToAction("Index");
}
return View(model);
}
Hope this can help you :)
Edit :
in my repository I do :
public void Add(Model.Models.LabExam.Examen entity)
{
using (var context = new PDSIDataContext())
{
var exam = BindModelExamenToRepExamen(entity);
context.Examen.InsertOnSubmit(exam);
context.SubmitChanges();
}
}
Binding methods (Repository.Examen represents my table, Repository is my project where I have a .dbml to represent my DB):
private static Repository.Examen BindModelExamenToRepExamen(Model.Models.LabExam.Examen modelExamen)
{
return new Repository.Examen
{
ID_Examen = modelExamen.ID,
ID_Examen_Type = modelExamen.ID_Examen_Type,
Date_Prescription = modelExamen.Date_Prescription,
Realise_Le = modelExamen.Realise_Le,
Statut = modelExamen.Statut,
Fait = modelExamen.Fait,
ID_Examen_Sous_Type = modelExamen.ID_Examen_Sous_Type,
ID_Examen_Sous_Sous_Type = modelExamen.ID_Examen_Sous_Sous_Type,
ID_Patient = modelExamen.ID_Patient,
Commentaires = modelExamen.Commentaires
};
}

MVC3 - Getting to Grips with the DropDownList

I'm new to MVC, so bear with me...
I've got my new form\view working (Creating and Adding a client)
But now I want to get the user so specifiy the Country where the new client is from A drop downlist. But im to sure how I to do this?
ViewModel
public class ClientNew
{
public string Company { get; set; }
public string Address { get; set; }
//New
public IEnumerable<CountryList> Country{ get; set; }
}
public class CountryList
{
public string Id { get; set; }
public string Name { get; set; }
}
Controller
(This is where is may be wrong, and is this the best way to do it?)
public ActionResult New()
{
var cl= new List<CountryList>();
cl.Add(new CountryList(){Id = "abcd",Name = "UK"});
cl.Add(new CountryList() { Id = "abce", Name = "USA" });
var model = new ViewModels.ClientNew();
model.Country= cl;
return View("New", model);
}
View (not sure how to plumb this in)
Html.DropDownList("Id" ??????)
In your view you will set up your dropdown on the property Id. This will be the current value selected in the dropdown when you POST to your form. The data that will be used for the dropdown is a SelectList called Countries that exists in your model.
#Html.DropDownListFor(m => m.Id, Model.Countries)
Your view model will have your Id, Name and Countries properties plus whatever else you need.
public class ClientNewViewModel {
public string Id { get; set; }
public string Name { get; set; }
public SelectList Countries { get; set; }
}
In your controller you need to pass the model to the view. You will need to populate the Countries SelectList. Keep in mind you will need to populate this value when you POST and fail validation as well.
public ActionResult New()
{
var model = new ClientNewViewModel();
model.Countries = new SelectList(service.GetCountries(),
"Id", "Name"); // set up what properties are used for id/name of dropdown
return View(model);
}
[HttpPost]
public ActionResult New(ClientNewViewModel model)
{
if( !ModelState.IsValid )
{
model.Countries = new SelectList(service.GetCountries(),
"Id", "Name");
return View(model);
}
// redirect on success
return RedirectToAction("Index");
}
Html.DropDownList("Id",
Country.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id
}));
There's a good blog post on the in's and out's of how to do this here -> http://277hz.co.uk/Blog/Show/10/drop-down-lists-in-mvc--asp-net

asp.net mvc viewmodels. How much logic (if any) should they contain

I've been looking into view models for mvc and I'm looking for the best way to do them. I've read loads of different articles but none seem to be clear as the "best way." So far example I might have a Customer model with the following properties:
First Name
Last Name
Title
Location
Where location is a foreign key to a location table in the database.
I want to be able to edit this customer but only the first name, last name and location. I'm not bothered about the title in the edit. So in my view I will need to pass a customer and a selected list.
Now from what I've read I have the following options (there's probably many more).
So my question is basically which is the best one?
1)
Add a select list to the ViewData["Location"] and just create a strongly typed view of customer?
2)
Create a view model where I pass a customer and select list (the data access is done in the controller):
public class ViewModelTest
{
public Customer Customer { get; set; }
public SelectList Locations { get; set; }
public ViewModelTest(Customer customer, SelectList locations)
{
Customer = customer;
Locations = locations;
}
}
3)
Create a view model where I pass a customer and list of locations and create the select list in the view model.
public class ViewModelTest
{
public Customer Customer { get; set; }
public SelectList Locations { get; set; }
public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation)
{
Customer = customer;
Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation);
}
}
4)
Pass a customer and repository and do the data access in the view model.
public class ViewModelTest
{
public Customer Customer { get; set; }
public SelectList Locations { get; set; }
public ViewModelTest(Customer customer, IRepository repository, string selectedLocation)
{
Customer = customer;
Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation);
}
}
5)
Create the view model with just the properties I need:
public class ViewModelTest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public SelectList Locations { get; set; }
public ViewModelTest(Customer customer, SelectList locations)
{
FirstName = customer.FirstName;
LastName = customer.LastName ;
Locations = locations;
}
}
6)
Or some other combination of the above or another way.
All opinions welcome.
Here's what I may suggest: have a view model which reflects the fields of strongly typed view:
public class SomeViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
public IEnumerable<SelectListItem> PossibleLocations { get; set; }
}
And in your controller action populate this view model:
public ActionResult Index()
{
var customer = Repository.GetCustomer();
var locations = Repository.GetLocations();
var viewModel = new SomeViewModel
{
FirstName = customer.FirstName,
LastName = customer.LastName,
Location = customer.Location,
PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location);
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(SomeViewModel viewModel)
{
// TODO: Handle the form submission
return View(viewModel);
}
Of course doing the mapping between the model and the view model manually as shown my example could become quite cumbersome and in this case I would recommend you looking at AutoMapper.
I'd have my ViewModel as this
public class SomeViewModel
{
public Customer Customer { get; set; }
public IEnumerable<Location> PossibleLocations { get; set; }
}
My controller like this:
public ActionResult Index()
{
var viewModel = new SomeViewModel
{
Customer = Repository.GetCustomer(),
PossibleLocations = Repository.GetLocations()
};
return View(viewModel);
}
and then you can access everything in your Customer object in the view like this:
Customer name - <%: Model.Customer.FirstName %> <%: Model.Customer.LastName %>
Location - <%: Html.DropDownList("LocationID", new SelectList(Model.PossibleLocations as IEnumerable, "LocationID", "LocationName", Model.Location.LocationID))%>

Resources