如何遍历相关模型以创建单个MVC视图 [英] How to iterate through related models to create a single MVC view

查看:80
本文介绍了如何遍历相关模型以创建单个MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是ASP MVC的新手,我正在做宿舍管理项目用于学习目的。我只使用ADO.NET(没有EF没有Linq)。我有两个模型居民和分配。我试图在同一视图中显示居民和分配(房间预订)。



在我的下面的代码中,我想显示来自Resident表的Resident信息(名字,姓氏和电子邮件),该表与Allocations表相关有关房间的信息。



在控制器中,我试图通过列表获取居民的信息然后检查,是否有任何分配给该居民的房间。



非常感谢任何帮助,提前谢谢。



我尝试了什么: < br $>


public ActionResult ManageResidents()

{

List< resident> mlist = new List< resident>();

ResidentDB db = new ResidentDB();

mlist = db.ResidentSelectAll();



foreach(mlist中的居民)

{

分配mAllocation = new Allocation();

AllocationDB db2 = new AllocationDB();

mAllocation =

db2.AllocationSelect(rs.ResidentAllocation.AllocationID);

rs.ResidentAllocation = mAllocation;

mlist.Add(rs);



}

ViewBag.Residents = mlist;

返回查看(mlist);

}

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);
}

推荐答案

如果您是MVC的新手,可以首先尝试脚手架来检查方案是否链接和工作。你需要遵循不同的教程。 google google google。

使用模型在初学者级别传递数据的最佳方式。

请查看以下链接



使用Model将数据传递给ASP.NET MVC中的View [ ^ ]



将数据从Controller传递到ASP.NET MVC中的View [ ^ ]
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[^]


这篇关于如何遍历相关模型以创建单个MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆