Click here to Skip to main content
15,909,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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);
    }
}

Please help me how to create view to display the list:
----------------------------
| StudentName | Major Name |
-----------------------------
| Mr. ABCDEFGHI | Computer |
--------------------------------
Thanks!
Posted

You have two tables named Student and Major... So,

SQL
SELECT Student.StudentName, Major.MajorID FROM Student INNER JOIN Major ON Student.MajorID = Major.MajorID


If any further query then feel free to ask.
 
Share this answer
 
v2
Comments
Bun Leap_kh 14-Feb-12 4:37am    
But I am using ADO.Net Entity Data Model.
Actually, I got the result in controller, but I couldn't create view.
I would like to know how to create view according to this result?
Thanks for your response!
 
Share this answer
 
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);
 
Share this answer
 

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