如何创建视图以显示以下控制器中的数据? [英] How to create view to display data from the below controller?
问题描述
我是ASP.Net的新手.我想从表Student(学生ID,StudentName,MajorID等)和Major(MajorID,MajorName等)中检索数据.
在我的控制器中:
I am new in ASP.Net. I would like to retrieve data from table Student (StudentID, StudentName, MajorID, ...) and Major (MajorID, MajorName, ...).
In my controller:
public class MajorController : Controller{
MajorEntity majorEntity = new MajorEntity();
StudentEntity studentEntity = new StudentEntity();
public ActionResult Index(){
var major = new List<MAJOR>(majorEntity.MAJORs);
var student = new List<SUTDENT>(studentEntity.STUDENTs);
var result = from s in student join m in major on s.MajorID equals m.MajorId select new {s.StudentName, m.MajorName };
return View(result);
}
}
请帮助我如何创建视图以显示列表:
----------------------------
|学生姓名|主要名称|
-----------------------------
| ABCDEFGHI先生|电脑|
--------------------------------
Please help me how to create view to display the list:
----------------------------
| StudentName | Major Name |
-----------------------------
| Mr. ABCDEFGHI | Computer |
--------------------------------
Thanks!
推荐答案
您有两个名为Student和Major的表.因此,
You have two tables named Student and Major... So,
SELECT Student.StudentName, Major.MajorID FROM Student INNER JOIN Major ON Student.MajorID = Major.MajorID
如果有其他疑问,请随时询问.
If any further query then feel free to ask.
此链接将为您提供帮助
http://msdn.microsoft.com/en-us/library/aa697427 (v = vs.80).aspx [
this link will help you
http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx[^]
您正在将对象集合传递给视图,因此您要做的就是读出它们并创建一张桌子.这是伪代码,因为我没有手头的所有文档,而且我也不太使用ASP.net,但是:
You''re passing the collection of objects to the view, so all you have to do is read them out and create a table. This is pseudocode because I don''t have all the docs to hand and I don''t use ASP.net that much, but:
<Page Class="System.Web.Mvc.Page<IEnumerable<StudentSubject>>"/>
<html>
<body>
<table>
<%
foreach(StudentSubject ss in Model)
Response.WriteLine("<tr><td>" + HtmlUtils.Escape(ss.StudentName) + "</td><td>" + ss.Subject + "</td></tr>");
%>
</table>
</body></html>
您还必须使用一个真实的类,而不是一个匿名类,因此您可以在视图中引用它:
You''ll also have to use a real class, not an anonymous one, so you can reference it in the view:
public class StudentSubject {
public string StudentName, Subject;
internal StudentSubject(string name, string subject){
this.StudentName = name; this.Subject = subject;
}
...
var result = from s in student join m in major on s.MajorID equals m.MajorId select new StudentSubject(s.StudentName, m.MajorName);
这篇关于如何创建视图以显示以下控制器中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!