0

I want to show unique values so that only unique link will be shown. what can i do please tell as soon as possible

[Here i want to show unique Value but here is repeatetion of values][1]

My controller is this

public ActionResult AdminStudentAttendance()
        {
            AdmissionDBEntities obj = new AdmissionDBEntities();
            var v = obj.AddTables.ToList();
            return View(v);
        }

My View is this

 @model IEnumerable<AMSPractice.Models.AttendanceIn>
    @using AMSPractice.Models;
<h2>ShowStudentAttendance</h2>

    @foreach (var a in Model)
    {
        @Html.ActionLink(a.RollNo,"OneStudentDates","Attendance", new { nid = a.RollNo }, null) <br />

    }

2 Answers2

0

Use Distinct to get unique values.

 var uniques= Model.Select(x => x.RollNo).Distinct().ToList();

@foreach (var a in uniques)
    {
        @Html.ActionLink(a,"OneStudentDates","Attendance", new { nid = a }, null) <br />

    }
Balaji Marimuthu
  • 1,940
  • 13
  • 13
  • Thanx for Solving my problem. – rohail khan Jan 06 '17 at 08:59
  • If i want to move nid as ID not as RollNo but Name shows as RollNo in actionlink then how can i do this.. becuase here actionlink name is rollno and id pass to controller also rollno. but in actionlink i want name as rollno and id pass to controller as ID. – rohail khan Jan 06 '17 at 15:49
0

Inside of your foreach, it looks like the only property of the model you care about is the RoleNo. Explore something like this:

@foreach (var roleNo in Model.Select(m => m.RoleNo).Distinct())
{
    @Html.ActionLink(roleNo,"OneStudentDates","Attendance", new { nid = roleNo }, null) <br />
}

You may need to play with that a bit, but basically you are appending a filter to Model to first give you only the RoleNo properties of each, then unique only.

  • thanx for solving my problem :-) – rohail khan Jan 06 '17 at 08:59
  • If i want to move nid as ID not as RollNo but Name shows as RollNo in actionlink then how can i do this.. becuase here actionlink name is rollno and id pass to controller also rollno. but in actionlink i want name as rollno and id pass to controller as ID – rohail khan Jan 06 '17 at 15:50
  • Check out this post: (http://stackoverflow.com/questions/2537823/distinct-by-property-of-class-by-linqt. Basically, you'll want to filter your list of models first, then send it into the foreach like you had in your original post. That way you'll have access to the entire object again, including the two properties you need. – Skyler Austin Jan 07 '17 at 00:17
  • i did not understand from this question can u explain further.. but according to my question my which i did. my problem i resolved so i tick on solved. – rohail khan Jan 08 '17 at 15:57