如何使用ASP.NET MVC中的两个表的数据填充视图 [英] how to populate a view using data of two tables in ASP.NET MVC
本文介绍了如何使用ASP.NET MVC中的两个表的数据填充视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个视图,显示单击选项卡时学生的完整信息。 Table1包含有关学生的信息,Table2包含学生的联系方式,我想显示来自table1的学生姓名和同一学生的table2的联系方式。我试过但是我没有得到结果。
型号
<前lang =c# > 命名空间 SA.Models
{
public 类 NewModel
{
public 列表< student> List1 { get ; set ;}
public 列表< detail> List2 { get ; set ;}
}
}
控制器
public ActionResult DisplayDetails()
{
Model1 studet = new Model1() ;
NewModel studet = new NewModel();
studet.List1 =(从db.students中的o
选择o).ToList();
studet.List2 =(来自db.Details中的p
select p).ToList();
return View(studet);
}
查看
@ model IEnumerable < SA.Models.NewModel >
< span class =code-keyword>< table >
< tr >
< th > < / th >
< / tr >
@foreach(模型中的var项目){
< tr >
< td < span class =code-keyword>>
@ Html.DisplayFor(m => item.List1)
@ Html.DisplayFor(m => item.List2)
< / td >
< td >
@ Html.ActionLink(编辑,编辑,新{/ * id = item.PrimaryKey * /})|
@ Html.ActionLink(详细信息,详细信息,新{/ * id = item.PrimaryKey * /})|
@ Html.ActionLink(删除,删除,新{/ * id = item.PrimaryKey * /})
< / td >
< / tr >
}
< / table >
解决方案
请参阅...
http://stackoverflow.com/questions/3311369/asp-net-mvc-displaying-results-from-two -table-on-one-view [ ^ ]
http://stackoverflow.com/questions/2011516/displaying-data-from-multiple -tables-in-a-view-as-a-list-asp-net-mvc [ ^ ]
http://stackoverflow.com/questions/ 4319488 / how-do-do-do-data-from-multiple-tables-in-a-mvc-view [ ^ ]
I have a view which shows complete information about the student when a tab is clicked. Table1 contains information about student and Table2 contains Contact details of student, I want to show student name from table1 and contact details from table2 of the same student. I tried but I'm not getting result.
Model
namespace SA.Models
{
public class NewModel
{
public List<student> List1{get;set;}
public List<detail> List2{get;set;}
}
}
Controller
public ActionResult DisplayDetails() { Model1 studet = new Model1(); NewModel studet = new NewModel(); studet.List1 = (from o in db.students select o).ToList(); studet.List2 = (from p in db.Details select p).ToList(); return View(studet); }
View
@model IEnumerable<SA.Models.NewModel>
<table>
<tr>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(m => item.List1)
@Html.DisplayFor(m=>item.List2)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
解决方案
See...
http://stackoverflow.com/questions/3311369/asp-net-mvc-displaying-results-from-two-tables-on-one-view[^]
http://stackoverflow.com/questions/2011516/displaying-data-from-multiple-tables-in-a-view-as-a-list-asp-net-mvc[^]
http://stackoverflow.com/questions/4319488/how-do-i-display-data-from-multiple-tables-in-a-single-mvc-view[^]
这篇关于如何使用ASP.NET MVC中的两个表的数据填充视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文