简体   繁体   中英

How to implement paging in mvc with sql query

Hi I have one query that is made in sql server as below and the sp name is as sp_StudentRequest

Select DISTINCT SR.StudentRequestId,SR.RegistrationId,SR.Location,SR.PaymentMethod,SR.CreatedOn,C.ClassName,CC.CampusName,
CASE WHEN ISNULL(TSR.StatusId,0)=0 THEN 1 ELSE TSR.StatusId END AS StatusId,
 substring(
(
    Select ', '+REPLACE(REPLACE(ST1.FromTime,'AM',''),'PM','')+'-'+ST1.ToTime AS [text()]
    From dbo.StudentRequestTimings ST1
    Where ST1.StudentRequestId = SRT.StudentRequestId
    ORDER BY ST1.CreatedOn
    For XML PATH ('')
), 2, 1000) [Time]
FROM StudentRequest SR
Inner JOIN Registration R ON R.RegistrationId=SR.RegistrationId
INNER JOIN Campus CC ON CC.CampusId=R.CampusId
INNER JOIN Class C ON C.ClassId=SR.ClassId 
LEFT JOIN TutorClasses TC ON SR.ClassId=TC.ClassId
LEFT JOIN StudentRequestTimings SRT ON SR.StudentRequestId=SRT.StudentRequestId
LEFT JOIN TutorStudentRequest TSR ON TSR.StudentRequestId=SRT.StudentRequestId AND TutorId=@RegistrationId
where TC.RegistrationId=@RegistrationId
ORDER BY SR.CreatedOn DESC

and I am calling this code from controller as

public ActionResult TutorDashboard(int? page)
        {               
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand("sp_StudentRequest"))
                        {

                            DataSet GetData = new DataSet();
                            cmd.Connection = con;
                            con.Open();
                            cmd.Parameters.AddWithValue("@Operation", "GetTutorRequestDetail");
                            cmd.Parameters.AddWithValue("@RegistrationId", 1);

                            cmd.CommandType = CommandType.StoredProcedure;
                            SqlDataAdapter ad = new SqlDataAdapter(cmd);
                            ad.Fill(GetData);
                            if (GetData.Tables[0].Rows.Count > 0)
                            {
                                ViewBag.TutorRequest = GetData.Tables[0].ToPagedList(page ?? 1, 1);
                            }  
                            con.Close();
                        }
                    }
            return View(ViewBag.TutorRequest);
        }

Now I need tom implement the paging in MVC with the help of PagedList, How Can I do so?

If I am not able to make use of this sql query then what is the way to convert this query in entity framework?

Any help will be useful to me.

To apply paging I had converted my query in entity frame work and below is the code

var query = (from sr in db.StudentRequests
             join r in db.Registrations on sr.RegistrationId equals r.RegistrationId
             join cc in db.Campus on r.CampusId equals cc.CampusId
             join c in db.Classes on sr.ClassId equals c.ClassId
             from tc in db.TutorClasses.Where(t => t.ClassId == sr.ClassId).DefaultIfEmpty()
             from srt in db.StudentRequestTimings.Where(s => s.StudentRequestId == sr.StudentRequestId).DefaultIfEmpty()
             from tsr in db.TutorStudentRequests.Where(t => t.StudentRequestId == srt.StudentRequestId && t.TutorId == registrationid).DefaultIfEmpty()
             where tc.RegistrationId == registrationid
             select new
             {
               StudentRequestId = sr.StudentRequestId,
               RegistrationId = sr.RegistrationId,
               Location = sr.Location,
               PaymentMethod = sr.PaymentMethod,
               CreatedOn = sr.CreatedOn,
               ClassName = c.ClassName,
               CampusName = cc.CampusName,
               StatusId = tsr.StatusId == null ? 1 : tsr.StatusId,
               Time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId).Select(p => p.FromTime.ToString().Replace("AM", "").Replace("PM", "") + "-" + p.ToTime)
               }).ToList().GroupBy(p => new { p.StudentRequestId }).Select(g => g.First()).ToList();


          var model = query.AsEnumerable().Select(x => new TutorDashboard
           {
              StudentRequestId = x.StudentRequestId,
              RegistrationId = x.RegistrationId,
              Location = x.Location,
              PaymentMethod = x.PaymentMethod,
              CreatedOn = x.CreatedOn,
              ClassName = x.ClassName,
              CampusName = x.CampusName,
              StatusId = x.StatusId == null ? 1 : x.StatusId,
              Time = string.Join(",", x.Time),
               }).ToList().ToPagedList(page ?? 1, 1);

And at last I had pass this model on view side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM