Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi,

I want a replacement of nested repeater in MVC 4.0.
Actually, My scenario is something like:

* I have a table "tbl_class" which contains contains records of all the classes(id, class, floor)
* I have another table "tbl_students" which contains records of all the student(id,
fName,lName,rollNo,Class....etc).
* I want to show records of all the student class wise.

Class I floor 0
----------------
student1
student2
student3
student4

Class II floor 0
----------------
student5
student6
student7
student8
.
.
.
.
.

Please suggest some solution.

Thank you,

Regards,
Ashish
Posted
Updated 10-Sep-14 2:18am
v2

1 solution

1.ASP.NET MVC is different then ASP.NET where there are built in controls that you can see also in design mode.

2.In MVC you should use razor code and HTML elements (like div) in order to build your views.

3.From the Controller you should pass the Model to the View, and the Model that must contains the data that you want to display. Then in the view you should use @foreach (razor view block of code) to built dynamically each HTML groups of controls for each element.

4.In your case the Model should use razor code like in the next example:
C#
public class StudentClassInfo
{
public string ClassName{get; set;}
public int ClassFloor{get;set;}
private List<StrudentInfo> _students= new List<StrudentInfo>();
public List<StrudentInfo> Students{ get {return _students;}}
//....

}

public class StudentInfo
{
public string Name{get;set;}
//...
}

5.In your view you should use your model data like in the next example:
HTML
@model List<StudentClassInfo>
<div>
@foreach (StudentClassInfo item in Model) {
     <div>Class @item.ClassName @item.ClassFloor<br/>
     <hr/>
     @foreach(StudentInfo studentInfo in item.Students)
     {
         @studentInfo.Name
         <br/>   
     }
     <br/>
     </div> 
}
</div>
 
Share this answer
 
v4
Comments
Aashish vermaa 10-Sep-14 8:36am    
But in my scenario class and students both comes in bulk.
so what will be the class and cshtml file?
Raul Iloc 10-Sep-14 8:50am    
1.You should get the data from the database and create the model objects from them and at the end to pass the Model as a "List" of "StudentclassInfo".
2.See my update above!
Aashish vermaa 12-Sep-14 2:50am    
It is giving the following error at list type property: must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors
Raul Iloc 12-Sep-14 2:53am    
1.I didn't give you all code, just the idea.
2.To fix it is simply just declare a variable of type "List of Student" and return it, see my update in the code above!
Aashish vermaa 12-Sep-14 4:00am    
I am doing like this:
controller code:
----------------
AssessmentClass lstAssessment = new AssessmentClass();

var resAssementSection = (from objSection in DbEntitiy.SECTIONS
join objAssementSection in DbEntitiy.LK_ASSESSMENTS_SECTIONS on objSection.section_id equals objAssementSection.section_id
where objAssementSection.assessment_id == 138
select new
{
objSection.section_id,
objSection.title
}).ToList();

if (resAssementSection.Count > 0)
{
for (int i = 0; i < resAssementSection.Count; i++)
{
//lstAssessment.section[i].sectionId = Convert.ToInt32(resAssementSection[i].section_id);
lstAssessment.section[i].title = resAssementSection[i].title;

var resSectionQuestion = (from objQuestion in DbEntitiy.QUESTIONS
join objSectionQuestion in DbEntitiy.LK_SECTIONS_QUESTIONS on objQuestion.question_id equals objSectionQuestion.question_id
where objSectionQuestion.section_id == resAssementSection[i].section_id
select new
{
objQuestion.question_id,
objQuestion.question1
}).ToList();
if (resSectionQuestion.Count > 0)
{
for (int j = 0; j < resSectionQuestion.Count; j++)
{
lstAssessment.section[i].question[j].questionId = resSectionQuestion[j].question_id;
lstAssessment.section[i].question[j].question = resSectionQuestion[j].question1;
}
}
}

Class file:
-----------
public class AssessmentSectionClass
{
public int sectionId { get; set; }
public string title { get; set; }
private List<sectionquestion> _SectionQuestion = new List<sectionquestion>();
public List<sectionquestion> question { get { return _SectionQuestion; } }
}

public class SectionQuestion
{
public int questionId { get; set; }
public string question { get; set; }
}

Now I am getting the following error when I assigning the question value to the property in the controller:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Please suggest, how I fixed it?

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