Passing value from one model to another in a ViewModel - c#

I am building my first MVC website and I am struggling to pass a value from one model to another in a ViewModel. The two classes are Music and MusicGenre. In Music, there is a field called 'GenreId' which is an int and is related to the 'Id' field in MusicGenres. I want to return the genre name relating to the Id that's passed over.
Here's what I've got so far:
MusicViewModel vmMusic = new MusicViewModel
{
Music = _context.Music.SingleOrDefault(c => c.Id == id),
MusicGenres = _context.MusicGenres.Where(gi => gi.Id == xxx).ToList()
};
return View(vmMusic);
This all renders nicely minus displaying the right genre. (I replaced xxx with Id but that just uses the id field from Music, not the GenreId field)
So xxx is where I want to pass 'GenreId' but I don't know how. Any help?
Thanks

To expand on my initial comment to the question, I'd just do something like:
var music = _context.Music.SingleOrDefault(c => c.Id == id);
var musicGenres = _context.MusicGenres.Where(gi => gi.Id == music.GenreId).ToList(); // whatever the genre id property is called.
MusicViewModel vmMusic = new MusicViewModel
{
Music = music,
MusicGenres = musicGenre
};
return View(vmMusic);

You should separate your view models from your persistence models. View model is designed to display what you want to display to the users. Persistence model might reflect your data storage structures.
Also from your example it looks like you might already have been using ORM like EntityFramework. EntityFramework provides features like navigation properties and lazy loading.
Either way, I strongly disagree accepted answer's approach, which sends the music and its genre persistence models to the view, when you only need to get the genre name.
Rather, I will approach it this way:
Persistence Models
You might have one-to-may relationship between a genre and an album in your persistence storage:
public class Album
{
public int AlbumId { get; set; }
public string Title { get; set; }
public double Price { get; set; }
public int GenreId { get; set; }
public Genre Genre { get; set; }
}
public class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
But obviously you don't want/need to reveal everything you have in your persistence storage to the user. Hence you create view models and only contain what you want to display.
View Models
Let's say you have a view to display information about the selected album. You might create a view model like this:
public class AlbumViewModel
{
public int AlbumId { get; set; }
public string AlbumName { get; set; }
public string Genre { get; set; }
}
Then in the album controller, you can build the view model by using any ORM, lazy loading, etc:
Controller
If you happen to use EntityFramework and have lazy loading enable, you can fetch genre name via its navigation property:
public ActionResult Details(int id)
{
var album = _dbContext.Albums.SingleOrDefault(x => x.AlbumId == id);
if (album == null)
{
return HttpNotFound();
}
var vm = new AlbumViewModel
{
AlbumId = album.AlbumId,
AlbumName = album.Title,
// You can get the genre name via Navigation property, and/or lazy
// loading
Genre = album.Genre.Name
};
return View(vm);
}
Now in a more advanced architecture, the read and write is separated, which is
referred as CQRS. For all the reads (i.e., displaying information to the user), you can build your view model with data directly from executing plain SQL statement.
CQRS with Dapper
using Dapper;
using System.Data;
...
public class AlbumController : Controller
{
private readonly IDbConnection _dbConnection;
public AlbumController(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
}
public ActionResult Details(int id)
{
const string sql = #"
SELECT a.AlbumId, a.Title AS [AlbumName], g.Name AS [Genre]
FROM [Album] a
JOIN [Genre] g ON a.GenreId = g.GenreId
WHERE a.AlbumId = #albumId;
";
var vm = _dbConnection.QuerySingleOrDefault<AlbumViewModel>(sql, new {
albumId = id
});
if (vm == null)
{
return HttpNotFound();
}
return View(vm);
}
}

Related

MVC Two controllers One View

I am trying to have a details view in an existing controller that joins two controllers and passes into the view, but everything i have tried so far has not worked. I have 4 data models and what i would like is to use 2 of them Company and Staff. So when i select details on a specific Company it will return all Staff associated to that Company in the same view.
HRDataModel class
public partial class HRDataModel : DbContext
{
public HRDataModel()
: base("name=HRDataModel")
{
}
public virtual DbSet<Company> Companies{ get; set; }
public virtual DbSet<Attribs> Attribs{ get; set; }
public virtual DbSet<Staff> Staffs { get; set; }
....
Company Data Model
[Table("Company")]
public partial class Company
{
public Company()
{
Staffs = new HashSet<Staff>();
}
public virtual ICollection<Staff> Staffs { get; set; }
public int companyId{ get; set; }
[Required]
[StringLength(10)]
public string companyName{ get; set; }
.....
Staff Data Model
public partial class Staff
{
public Staff()
{
Skills = new HashSet<Skill>();
}
public virtual Company Company{ get; set; }
public virtual ICollection<Skill> Skills { get; set; }
public int staffId { get; set; }
.........
And i am trying to get my Details method in CompanyController to show details of all active Companies in the db and also all Staff attached to that Company
[Route("Company/Staff/1}")]
public ActionResult Details(int id)
{
Company co = db.Companies.Find(id);
...How to implement????
return View(bu);
}
If anyone can point me in the right direction that would be great. I have tried and tried but cannot get anything to work?
Since Company includes Staff you can use the include method to include related entities.
var company = db.Companies.Include(c => c.Staffs).FirstOrDefault(x => x.id == companyId);
return View(company);
And in your view:
#foreach(var staff in Model.Staffs) { ... }
You need to pass a data structure which has the company info and staff details to your view. You may pass your existing Comapny entity class to do this. But the problem is, It makes your razor view tightly coupled to your Entity which was generated by the ORM. What if you switch your Data access layer to something else tomorrow. So this solution is not great.
So you should use a view model ( A simple POCO class) which has properties which you need to render in the view. Then read your entity from db in your action method, map it to a vie wmodel instance and send it.
Create a view model like this.
public class CompanyInfo
{
public int Id {set;get;}
public string Name {set;get;}
public List<StaffItem> Staffs {set;get;}
public CompanyInfo()
{
this.Staffs = new List<StaffItem>();
}
}
public class StaffItem
{
public int Id {set;get;}
public string Name {set;get;}
}
In your action method read the Company entity and map it to the view model
public ActionResult Details(int id)
{
var vm = new ComapnyInfo();
var company = db.Companies
.Include(r => c.Staffs)
.FirstOrDefault(x => x.companyId==id);
if(co!=null)
{
//Map the property values now
vm.Name = co.companyName;
vm.Id = co.companyId;
if(co.Staffs!=null)
{
vm.Staffs = co.Staffs.Select(f=> new StaffItem {
Id=f.staffId,
Name = f.staffName}).ToList();
}
}
return View(vm);
}
Now your view should be bound to the CompanyInfo view model
#model YourNamespace.CompanyInfo
<h2>#Model.Name</h2>
<h3>Staffs</h3>
#foreach(var staff in ModelStaffs)
{
<p>#staff.Name</p>
}
If you do not like the manual mapping, You may consider using a mapping libarary like Automapper.
Hi #stackface you dont pass two controllers to get both views for that what you do is create one View Model which is essentially a container for multiple models and pass that into the view from the controller.
E.g. Model 1, Model2, ModelN all are needed so you have a class and in that class it has properties consisting of Model1, Model2 and Model3 so that way you pass in your class which has all the needed models.
E.g.
public class Company{...}
public class Staff{...}
public class ViewModel{
public Company Company {get;set;}
public List<Staff> Staff{get;set;}
}
controller:
{
var viewModel = new ViewModel();
viewModel.Company = db.Companies.FirstOrDefault(x => x.id == companyId);
viewModel.Staff = db.Staff.Where(x => x.CompanyId == campanyId).ToList() //or whatever your properties are called.
Return View(viewModel);
}
Update your view to take type ViewModel.
You can also compose a view by calling controller actions that return partial views.
The advantage is that you can reuse the partial views and their actions, e.g. to show the company details with the same layout on different pages. This increases consistency and maintainability.
The drawback is that you loose flexibility: if a certain page requires a different layout, you should create a new view. Performance might also be lower because you hit the backend with many small operations instead of a single big one.
The combined viewmodel to show both company and staff details only needs to know how to access the required data:
public class CompanyAndStaffDetailsViewModel {
public long CompanyId { get; set; }
public long StaffId { get; set; }
}
The following action renders the combined view:
public ActionResult Details(long companyId, long staffId) {
var viewModel = new CompanyAndStaffDetailsViewModel {
CompanyId = companyId,
StaffId = staffId
};
return View("Details", viewModel);
}
The "Details" View composes the usecase by calling actions to render partial views:
#model CompanyAndStaffDetailsViewModel
#Html.Action("CompanyInfoPartial", "Company", new { companyId = Model.CompanyId })
#Html.Action("StaffInfoPartial", "Staff", new { staffId = Model.StaffId })
The "Company" controller provides a reusable action to render company details:
public ActionResult CompanyInfoPartial(long companyId) {
Company co = db.Companies.Find(companyId);
var model = Mapper.Map<CompanyViewModel>(co); // map persistable entity to ViewModel
return PartialView("_CompanyInfoPartial", model);
}
Then the parital View _CompanyInfoParital.cshtml only has to deal with the Company Info stored in the CompanyViewModel and knows nothing about Staff:
#model CompanyViewModel
#Html.DisplayFor(m => m.CompanyName)
// etc ...
The idea for the StaffInfoPartial action is the same as for CompanyInfoPartial.

Populate associated entities on DBContext Find

I have a Product which needs to have some fields in multiple languages. Therefore I have made a ProductLanguage table which has the composite key and language specific fields (ProductID, LanguageID, Name).
In my Product class I tried something like this:
[Table("Product")]
public class Product
{
DBContext db = new DBContext();
public Product()
{
this.Multimedias = new List<Multimedia>();
this.ProductLanguages = new List<ProductLanguages>();
this.ProductLanguage = db.ProductLanguages.Find(this.ID, Global.Language) ?? new ProductLanguage();
}
public int ID { get; set; }
public string Code { get; set; }
public virtual ICollection<Multimedia> Multimedias { get; set; }
public virtual ICollection<ProductLanguage> ProductLanguages { get; set; }
[NotMapped]
public virtual ProductLanguage ProductLanguage { get; set; }
}
So I could immediately access the language specific fields without needing to go through the collection - the problem is the object obviously doesn't have the ID yet.
Is there any way so when I do
Product product = db.Products.Find(id);
in the controller it will automatically populate my ProductLanguage property?
You can move your assignment of ProductLanguage in the Get for that property.
You don't need the property
[NotMapped]
public virtual ProductLanguage ProductLanguage { get; set; }
The ProductLanguages collection will be populated via lazy loading when you hit it. What you need is a method like this, that will return a ProductLanguage by id:
public ProductLanguage GetProductLanguageById(int id)
{
if (ProductLanguages != null)
{
return ProductLanguages.Where(pl => pl.Id == id).FirstOrDefault();
}
}
This is not confirming to any practice/usage i have seen before.., regardless I would consider it very!! very!! bad practice. Don't make an instance of your context within the Entity(table).
Also why are you doing this... I suggest you read up on Lazy and eager loading.
Quote
"in the controller it will automatically populate my ProductLanguage property?"
Yes.... use eager loading.
Product product = db.Products.Include("ProductLanguage").Find(id);
But I would highly suggest that you don't do all that other weird stuff in the initialization of your entity.
DBContext db = new DBContext();
Product product = db.Products.Include("ProductLanguage").Find(id);

View using DTO - ASP.NET MVC

I'm fairly new to MVC and I've been trying to create a view using a DTO as the Model Class, but it seems to be using the Data Context class I use for my Models, even though I am clearing the selection when I am creating the view.
This issue seems to be causing a NullReferenceException which is caused by the following exception being thrown and the view not having any returned to it.
ITSSkillsDatabase.Models.PersonSkillSetsDTO: : EntityType 'PersonSkillSetsDTO' has no key defined. Define the key for this EntityType.
PersonSkillSets: EntityType: EntitySet 'PersonSkillSets' is based on type 'PersonSkillSetsDTO' that has no keys defined.
My DTO:
namespace ITSSkillsDatabase.Models
{
public class PersonSkillSetsDTO
{
public int IDSkillset { get; set; }
public int IDCategory { get; set; }
public string Product { get; set; }
public string P_Version { get; set; }
public string Notes { get; set; }
public int PersonSkillsID { get; set; }
public int IDPerson { get; set; }
public int Score { get; set; }
public DateTime ScoreDate { get; set; }
public int TargetScore { get; set; }
public DateTime TargetDate { get; set; }
public DateTime RefresherDate { get; set; }
}
}
Controller method:
public ActionResult SkillSets(int? id)
{
try
{
if (id == null)
{
return HttpNotFound();
}
var viewModel = (from a in db.SkillSets
join c in db.PersonSkills on a.IDSkillset equals c.IDSkillSet
where c.IDPerson == id
select new Models.PersonSkillSetsDTO
{
IDSkillset = a.IDSkillset,
IDCategory = a.IDCategory,
Product = a.Product,
P_Version = a.P_Version,
Notes = a.Notes,
PersonSkillsID = c.PersonSkillsID,
IDPerson = c.IDPerson,
Score = c.Score,
ScoreDate = c.ScoreDate,
TargetScore = c.TargetScore,
TargetDate = c.TargetDate,
RefresherDate = c.RefresherDate
}).ToList();
return View(viewModel);
}
catch
{
return View(); //this is where the NullReferenceException is thrown
}
}
These are the settings when I'm creating the view:
I realise I can get rid of the NullReferenceException by checking for null values, but I don't have any idea how to fix the issue with my DTO.
I am going to try to explain using a ViewModel/DTO to create a Form and POST back.
ViewModels are outside of the Database Context, So if you are using a ViewModel you have to Map your data from ViewModel to Model and Model to ViewModel.
So if you are reading from Database
Create DBContext
read data you want to read
Map to a ViewModel
Pass ViewModel to the View or API
If you are writing to the database
POST ViewMdoel from View to Controller (You can use Ajax)
Create DBContext
Map from ViewModel to Model
Save Model to Database
let's say you have a DTO,
public class CountryDTO
{
public int CountryId { get; set; }
[Display(Name = "Country Name")]
[Required(ErrorMessage = "This field is required")]
public string CountryName { get; set; }
[Display(Name = "Latitude")]
[Required(ErrorMessage = "This field is required")]
public double CentralLat { get; set; }
[Display(Name = "Longitude")]
[Required(ErrorMessage = "This field is required")]
public double CentralLang { get; set; }
[Display(Name = "GMT Offset")]
[Required(ErrorMessage = "This field is required")]
public double GMTOffSet { get; set; }
[Display(Name = "Currency")]
[Required(ErrorMessage = "This field is required")]
public string Currency { get; set; }
}
Create a controller i.e. CountryController and you have a Views Folder Country, Right Click Country Folder Add --> View, Name it CreateCountry and Select Model to be CountryDTO
You can't select DataContext here , because DTO is not part of the Context
This will create your view with Fields from the DTO.
Now in your Controller you need 2 Actions
GET method to return the View
POST method to POST back the form
public ActionResult CreateCountry()
{
return PartialView(new CountryDTO());
}
Now in the POST method you will Pass the DTO, Let's assume you have a Country table in your Database, you will have to create a New Country type Object and add to the Context
[HttpPost]
public ActionResult CreateCountry(CountryDTO model)
{
if (ModelState.IsValid)
{
// Model State is Valid
// here you will create Context
using (var dbContext = new DATBASE_CONTEXT())
{
var newCountry = new Country() // Country is a Model from Database
{
CountryName = model.CountryName,
CentralLat = model.CentralLat,
// Map All Properties from View Model to Model
};
// Add the New Country to the Countries
dbContext.Countries.Add(newCountry);
// Save Database Changes
dbContext.SaveChanges();
}
}
return PartialView(model);
}
IF you want to display this Country:
public ActionResult CountryDetails(int id)
{
var model = new CountryDTO();
using (var dbContext = new DATABASE_CONTEXT())
{
var country = dbContext.Country.First(s => s.CountryId == id);
model.CountryName = country.CountryName;
// Same for other Properties
// You can use AutoMapper Library to Map from Model to DTO/ViewModel
}
return View(model);
}
try
{
// <...>
return View(viewModel);
}
catch
{
return View(); //this is where the NullReferenceException is thrown
}
You get NullReferenceException because your view expects the model and doesn't perform null checks in Razor.
To verify this, you can create a dummy model and pass it into your return View(/* model */); call.
As I understood the exception trouble is not with DbContext but with lame model: "No key defined". I think checking data-annotations might help.
I created a new View Model which contains everything I need from the two models.
I then set the values for the View Model within the controller.
Which gave me the result I wanted.

A specified Include path is not valid. The EntityType 'SpiceShop.Models.Product' does not declare a navigation property with the name 'Products'

I Have Database that contains 2 tables
TABLE Categories (CategoriesId, Name, Description)
TABLE Products (ProductId, CategoriesId, Title, ProductImageUrl)
Categories is linked to Products by CategoriesId.
I am trying to use LINQ to get all of a particular Title.
public ActionResult Browse(string categories)
{
var spices = spiceDB.Products.Include("Products").Single(p => p.Title == categories);
return View(spices);
}
Product Model
namespace SpiceShop.Models
{
public class Product
{
[Key]
public int ProductId { get; set; }
public int CategoriesId { get; set; }
public string Title { get; set; }
public string ProductImageUrl { get; set; }
public List <Categorie> Name { get; set; }
}
}
Categorie Model
namespace SpiceShop.Models
{
public class Categorie
{
[Key]
public int CategoriesId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
//public List<Product> ProductId { get; set; }
public List<Product> Products { get; set; }
}
}
Just remove the .Include("Products"), that's not how it's used.
The error is clear, there is no "Products" property at your "Product" model.
Edit:
It looks like your View requires a Model that is a single object of type "Product".
Also, I have tried to give each of my identifiers/variables a name that best conveys their meaning and intent.
Edit 2:
Please note that changing any input parameter names on a method will require a corresponding change in your View code that calls the method.
In MVC, Action-Method parameters are automatically mapped.
Your debug shows a URL of Browse?categories=Sauces, which is automatically mapped to the method "Browse" with input parameter categories set to "Sauces". But (my version of) the Browse method is expecting a categoryName parameter, not categories. So you will need to make sure that URL Attribute and the Method Parameter have exactly the same name.
So if you really need to pass the selected Category's Name as the input parameter:
public ActionResult Browse(string categoryName)
{
// Gets the CategoryId for the given categoryName, or zero if not found.
var categoryId = spiceDB.Categories
.Where(c => c.Name == categoryName)
.Select(c => c.CategoriesId)
.FirstOrDefault();
// Gets the first Product (only) for the specified categoryId.
var firstProduct = category.Products
.FirstOrDefault(p => p.CategoriesId == categoryId);
return View(firstProduct);
}
However, a much more common usage scenario for supporting this type of "parent-child" relationship would be:
Your Category List invokes a query by ID when clicked, not a query by Name (i.e. 3, not "Pickles")
Your View supports all the related Products, not just the first one (i.e. a model of type List<Product>, not just Product)
e.g.
public ActionResult Browse(int categoryId)
{
var products = spiceDB.Products
.Where(p => p.CategoriesId == categoryId)
.ToList();
return View(products);
}
i.e. making your Product List more comprehensive, and your data access code simpler and more efficient.

LINQ to Entities query error

I am encountered an error that I am not familier with. I tried to google with no success.
I wrote the following query where I am having this error.
The entity or complex type 'MyWebProject.Models.UserDetail' cannot be constructed in a LINQ to Entities query.
The query:
UsersContext db = new UsersContext();
var userdata = (from k in db.UserDetails
where k.UserId == WebSecurity.CurrentUserId
select new UserDetail()
{
FullName = k.FullName,
Email = k.Email,
About = k.About,
Link = k.Link,
UserSchool = new School()
{
SchoolId = k.UserSchool.SchoolId,
SchoolName = k.UserSchool.SchoolName
},
UserCourse = new Course()
{
CourseId=k.UserCourse.CourseId,
CourseName=k.UserCourse.CourseName
},
Country=k.Country
}).FirstOrDefault();
Class:
public class UserDetail
{
public int Id { get; set; }
public int UserId { get; set; }
public string FullName { get; set; }
public string Link { get; set; }
public bool? Verified { get; set; }
public string Email { get; set; }
public string About { get; set; }
public School UserSchool { get; set; }
public Course UserCourse { get; set; }
public string Country { get; set; }
}
public class School
{
public int SchoolId { get; set; }
public string SchoolName { get; set; }
public string Country { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public School School { get; set; }
}
Any idea what went wrong??
It looks like it is due to how you are creating the complex properties School and Course in the middle of the query. It would be better to select the User (remove the select transformation), then use navigation properties to access those objects instead of building them manually. The navigation are meant for this as long as you have the proper relations built with foreign keys.
UsersContext db = new UsersContext();
var userdata = (from k in db.UserDetails
where k.UserId == WebSecurity.CurrentUserId})
.FirstOrDefault();
// access navigation properties which will perform the joins on your behalf
// this also provides for lazy loading which would make it more effecient. (it wont load the school object until you need to access it)
userdata.School
userdata.Course
MSDN article about navigation properties: http://msdn.microsoft.com/en-us/library/vstudio/bb738520(v=vs.100).aspx
This should give you what you want. It will load your objects as part of the query (and not rely on lazy loading).
UsersContext db = new UsersContext();
var userdata = db.UserDetails.Include(x => x.UserSchool)
.Include(x => x.UserCourse)
.Include(x => x.Country)
.Where(x => x.UserId == WebSecurity.CurrentUserId)
.FirstOrDefault();
I think it's because your entity has the same name of the object you're trying to create. Try renaming the object you want to return back. If you want to return the same type as your entity try the eager loading with .Include("relationshipname") feature.
A great answer from #Yakimych is given below.
You cannot (and should not be able to) project onto a mapped entity. You can, however, project onto an annonymous type or onto a DTO:
public class ProductDTO
{
public string Name { get; set; }
// Other field you may need from the Product entity
}
And your method will return a List of DTO's.
public List<ProductDTO> GetProducts(int categoryID)
{
return (from p in db.Products
where p.CategoryID == categoryID
select new ProductDTO { Name = p.Name }).ToList();
}
Mapped entities in EF basically represent database tables. If you project onto a mapped entity, what you basically do is partially load an entity, which is not a valid state. EF won't have any clue how to e.g. handle an update of such an entity in the future (the default behaviour would be probably overwriting the non-loaded fields with nulls or whatever you'll have in your object). This would be a dangerous operation, since you would risk losing some of your data in the DB, therefore it is not allowed to partially load entities (or project onto mapped entities) in EF.
For more details please go to the following link:
The entity cannot be constructed in a LINQ to Entities query

Categories

Resources