How to display columns from two tables in MVC? - asp.net-mvc

Right. So I am new to MVC.
I have two tables. tblCustomer_Details and tblCustomer_Location. There is a relationship between the two tables by CustomerID.
Now, how do I display few columns from one table and few from others?
I have tried using View Model. I created a new model named ViewModel and added Lists of both tables.
How do I proceed from here?

Related

Can't use my joining table from DbContext

I have database with different tables. But I can't get access to some tables, and I'm curious why? For example, there is Projects table and Users table in database, and I need to get list of all users that are in specific Project by Id. These tables have many to many relationship, so there is joining table with ProjectId and UserId. It's logic to use some Join to get users that I need from table Users with help of this joining table from db. But how can I make Join when my DbContext doesn't have joining table? How can I get access to joining table using DbContext, or maybe there are some different ways to get list of users by project id?
If someone have similar problem, here is a simple way to use all the data from collections in model
var project = _db.Projects.Include(x => x.Tickets).ThenInclude(y => y.Users).SingleOrDefault(p => p.Id == id);
so after that you gonna have your model and all the data from collections that you need
ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data.
So you can will have collection of users for this project. And doing filtering by users from your Ticket (example from image).
Yes, exists few different ways - setting with ef fluent, or attribute, or dbcontext. Better to find complete guide for many-to-many setting in your application.

rails, a model based on a custom SQL query

Say that i have a table that is a group by on many base tables on my database.
I can implement a database view and so i can then base a model on it as i would do with a regular, 'physical' table.
Is it possilbe also to specify the sql query of the view directly in the model, so without creating the view at all?

Multiple DropDownLists and a View and View model

A design question on multiple DropDownLists on a View.
What is the best approach to displaying multiple DropDownLists on a View? Is it possible to pass multiple DropDownLists to a View model?
For example:
If I had the following View models: Student, Course, and Enrollment. And on the Index View page, I'm returning and displaying all students. But I'd like to filter the number of Students being returned based on values selected from multiple DropDownLists, such as Suburb, LanguageSpoken...
Each DropDownList will be populated from the database.
Two approaches that I can think:
In the Student View model, have a (fully populated) collection of Suburb, LanguageSpoken... .
Have multiple Models passed to the View model. In this case one Model will be a Student, and the other Models will be each DropDownLists. I haven't looked into the details of this yet.
I don't like the first approach, as it sounds very inefficient, i.e. each Student hold collections of list of all Suburbs, Languages... Also, not sure if the second approach is possible or even a good idea.
I'm using ASP.NET MVC 5 and Entity Framework 6.
Ok, after alot of reading up and trying a number of things, I've finally have a (simple) solution: Drop Down Lists using ViewBag.
E.g. the Student controller returns a Student viewmodel to the View, and any further models such as postcodes, languages... are returned via ViewBag to the View.
I figure ViewBag is appropriately used for such data, i.e. lookup (static) type data.

how to retrive data from more than one table in mvc asp.net

I want to show list of order details including count of ordered material and name of customer.
There are three tables order_details, order_item_details and customer_details
for this scenario how to create view model and linq
my db structure
customer_details === customer_id,customer_name
|||||
order_details === order_id, order_number, customer_id
|||||
order_item_details === order_item_id, order_id , item_id
and i want to show------
order_details.order_number, customer_details.customer_name, count(order_item_details.order_item_id)
Create a view in your database that links the three tables appropriately, then point your ORM (EF?) at the view, and you will get a read-only view of your table with appropriate models. If what you're doing is particularly complex, you can create a stored procedure and use the same technique.
If you are in fact using Entity Framework, here's an SO answer that tells you how to create custom entities in EF 4 (first answer by Kevin Castle):
Entity framework 3.5, mapping stored procedure results to custom entity

Hibernate many-to-many mapping

I am one of hibernate user in Australia. Recently, I have been dealing with hibernate many-to-many mapping and not going well though.
I got in trouble with "join/associate table with extra columns mapping".
Let`s say there are three tables called Product Order and OrderProduct (includes extra column quantity). Product holds many-to-many relationship with Order.
My confusion is that do we have to consider both ends of associate table when we are writing mapping files? or just write either side?
Also, is it necessary to produce mapping file for the associate table as well?
Any suggestions will be appreciated!!
create a view to join two tables. this view and rest table is many to one relationship
Hope it's userful
You should have an association mapping and entity because you need to create them just like an other entity.

Resources