0

My controller code is like I am returning data as

public ActionResult StudentDashboard(int? page)
{    
    ViewBag.StudentRequest = GetData.Tables[0].AsEnumerable().Select(t => new
                                {
                                    StudentRequestId = t.Field<int>("StudentRequestId"),
                                    ClassName = t.Field<string>("ClassName"),
                                    CreatedOn = t.Field<DateTime>("CreatedOn"),
                                    Location = t.Field<string>("Location"),
                                    PaymentMethod = t.Field<string>("PaymentMethod"),
                                }).ToList().ToPagedList(page ?? 1,1);

    return View(db.StudentRequests.Where(x => x.RegistrationId ==  registrationid).ToList().ToPagedList(page ?? 1, 3));
}

Also how can I add inner join as class table in this code?

db.StudentRequests.Where(x => x.RegistrationId ==  registrationid)
                  .ToList()
                  .ToPagedList(page ?? 1, 3)

and on view side I have written code as

@using PagedList;
@using PagedList.Mvc;
@model IPagedList<Student_Tutor.Models.StudentRequest>

   @if (ViewBag.StudentRequest != null)
    { 
      var StudentRequestId = (int)Model.First().StudentRequestId;// Here I am able to get the StudentRequestId 
      var StudentRequestTimecount = StudentRequestTime.Where(d => d.StudentRequestId == StudentRequestId).ToList();
      var TutorStudentRequestcount = TutorStudentRequest.Where(d => d.StudentRequestId == StudentRequestId).ToList();
      @Html.LabelFor(model => model.First().StudentRequestId)// here only text is displaying as StudentRequestId
      @Html.DisplayNameFor(Model => Model.First().CreatedOn)//here only text is diplaying as created on
}

How can I get StudentRequestId record instead of only text at

@Html.LabelFor(model => model.First().StudentRequestId)

Through this code I am not able to get the Id number of StudentRequestId instead of number the view side is showing only text.

Please see this screenshot:

enter image description here

Xtremcool
  • 165
  • 3
  • 25

1 Answers1

1

The LabelFor helper method render a label element with the name of the property or the DisplayName property if it was used to decorate the property.

If you want to print the value of the property, you may use DisplayFor helper method.

@Html.DisplayFor(model => model.First().StudentRequestId)
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I had also tried using display for but still it was showing me the same text – Xtremcool Nov 06 '17 at 15:22
  • I had tried for created on too but it is showing text only – Xtremcool Nov 06 '17 at 15:22
  • If the `StudentRequestId` has a valid value, The `DisplayFor` helper will render that. – Shyju Nov 06 '17 at 15:23
  • I just tried your code with `DisplayFor` in the view and it did print the value of it. – Shyju Nov 06 '17 at 15:35
  • In my question I had also written with comments that on this line var StudentRequestId = (int)Model.First().StudentRequestId;// Here I am able to get the StudentRequestId .....I am able to get the value so what might be wrong that it is not displaying that value in DisplayFor – Xtremcool Nov 06 '17 at 16:20
  • Have tried `DisplayNameFor` or `DisplayFor` ? In your question you are using `DisplayNameFor`. You should use `DisplayFor` – Shyju Nov 06 '17 at 16:22
  • that working, Can u help for second query too, that I had written in question only for inner join – Xtremcool Nov 06 '17 at 16:32
  • Did you take a look at [Perform inner joins](https://learn.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins) – Shyju Nov 06 '17 at 16:34
  • hey I have multiple row data in db. then why it is showing retriving only single data on view side? – Xtremcool Nov 06 '17 at 16:38
  • Because your code is using the `First()` method which takes the first item from the collection. Use a `foreach` loop and render data inside that. – Shyju Nov 06 '17 at 16:39
  • u mean foreach(var r in model.row){ }.....Sorry I am new in MVC so I really need help – Xtremcool Nov 06 '17 at 16:42
  • as u suggested I had tried var query = from sr in db.StudentRequests join c in db.Classes on sr.ClassId equals c.ClassId select new { sr.StudentRequestId,sr.ClassId }; return View(query.ToList().ToPagedList(page ?? 1, 2)); but it giving error as invalid operation – Xtremcool Nov 06 '17 at 16:47
  • I suggest you post that as a new question with the specific code you tried for the JOIN and the error message you are getting. – Shyju Nov 06 '17 at 16:48
  • https://stackoverflow.com/questions/47142158/how-should-i-pass-my-query-from-controller-using-topagedlist – Xtremcool Nov 06 '17 at 17:05
  • can you look for https://stackoverflow.com/questions/47154713/how-to-implement-paging-in-mvc-with-sql-query this link too? – Xtremcool Nov 07 '17 at 11:13