Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone, I am new to ASP MVC, I am doing the Hostel Management project for learning purposes. I am only using ADO.NET (no EF no Linq). I have two Models Residents and Allocations. I am trying to show the Residents and Allocations (room booking) in the same view.

In my code below, I want to show Resident's information (first name, last name, and email) from Resident's table which is related to the Allocations table, which holds the information about rooms.

In the controller, I am trying to get the resident's information using lists and then check, is there any room allocated to that resident or not.

Will greatly appreciate any help, thanks in advance.

What I have tried:

public ActionResult ManageResidents()
{
List<resident> mlist = new List<resident>();
ResidentDB db = new ResidentDB();
mlist = db.ResidentSelectAll();

foreach (Resident rs in mlist)
{
Allocation mAllocation = new Allocation();
AllocationDB db2 = new AllocationDB();
mAllocation =
db2.AllocationSelect(rs.ResidentAllocation.AllocationID);
rs.ResidentAllocation = mAllocation;
mlist.Add(rs);

}
ViewBag.Residents = mlist;
return View(mlist);
}
Posted
Updated 25-Sep-18 9:56am
v6
Comments
F-ES Sitecore 25-Sep-18 4:41am    
You're going to need a view to show the data. If you don't even know how to show data on the page then I think you should spend more time learning the basics of MVC, you can't learn such a broad technology from asking questions on a forum. Get a book on MVC or go through tutorials like "MVC Book Store". You'll probably struggle to find book\tutorials that use ado.net, but that's not too relevant. You can use EF to go through the book\tutorials then just use ado.net in your actual project once you understand the MVC part of the equation.

1 solution

If you are new to MVC you can try first the scaffolding to check hows scenarios are linked and working. You need to follow different tutorial. google google google.
The best way to use model to pass data on beginner level.
Please check following links

Using Model to pass data to View in ASP.NET MVC[^]

Passing data from Controller to View in ASP.NET MVC[^]
 
Share this answer
 
Comments
Member 1565922 25-Sep-18 7:36am    
Thanks for the reply, I am trying to pass muliple Models to a signke view. In the above case I have two tables Residents and Allocations, please see code i wrote in the controller, First I get all the Residents and then trying to get room allocations from the allocations table,that's where I am struggling
summiya1 25-Sep-18 9:49am    
Never add query inside loop it will effect the performance.If you have two tables then you can use join on Allocation table to get the relevant result once and pass it to view using view model.
Member 1565922 25-Sep-18 10:40am    
Any idea how I can get the desired output?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900