本文介绍了如何在不重新加载页面的情况下在MVC中进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过这段代码



控制器:





公共ActionResult索引(字符串排序顺序)

{

visitorListBL = new VisitorListBL();

// List< visitordm> visitorDM = visitorListBL.GetCheckInVisitorList();

#region由Rajesh Mintri创建进行排序

var visitorDM = visitorListBL.GetCheckInVisitorList()。AsEnumerable();

ViewBag.NameSort = String.IsNullOrEmpty(sortorder)? name_desc:;

ViewBag.InTimeSort = sortorder ==Date? date_desc:日期;

ViewBag.OfficeLocationSort = sortorder ==OfficeLocation? OfficeLocation_desc:OfficeLocation;

ViewBag.EmployeeNameToVisitSort = sortorder ==EmployeeNameToVisit? EmployeeNameToVisit_desc:EmployeeNameToVisit;

ViewBag.MobileNumberSort = sortorder ==MobileNumber? MobileNumber_desc:MobileNumber;

ViewBag.VisitIdSort = sortorder ==VisitId? VisitId_desc:VisitId;

开关(sortorder)

{

casename_desc:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.Name);

break;



caseDate:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderBy(objvisitor => objvisitor.Office_in_time);

break;

casedate_desc:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.Office_in_time);

break;

caseOfficeLocation:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderBy(objvisitor => objvisitor.OfficeLocation);

break;

caseOfficeLocation_desc:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.OfficeLocation);

break;

caseEmployeeNameToVisit:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderBy(objvisitor => objvisitor.EmployeeName_to_visit);

break;

caseEmployeeNameToVisit_desc:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.EmployeeName_to_visit);

break;

caseMobileNumber:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderBy(objvisitor => objvisitor.Mobile_number);

break;

caseMobileNumber_desc:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.Mobile_number);

休息;

案例VisitId:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderBy(objvisitor => objvisitor.Visit_id);

休息;

案例VisitId_desc:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.Visit_id);

break;

默认值:

visitorDM = visitorListBL.GetCheckInVisitorList()。OrderByDescending(objvisitor => objvisitor.Office_in_time );

休息;

}

#endregion

返回查看(visitorDM);

}





查看:



@foreach(var型号中的项目)

{

var ItemId =VisitorChecKedOut-+ @ item.Visit_id;

}

@ *访问ID * @

@ Html.ActionLink(访问ID,排序,n ew {sortorder = ViewBag.VisitIdSort})



@ *办公地点* @

@ Html.ActionLink (办公地点,排序,新{sortorder = ViewBag.OfficeLocationSort})

@ *访客姓名* @

@Html .ActionLink(访客姓名,排序,新{sortorder = ViewBag.NameSort})

@ *访客手机号码* @

@ Html.ActionLink(访客移动电话号码,排序,新{sortorder = ViewBag.MobileNumberSort})

@ *会见的人* @

@ Html.ActionLink(人物待遇,排序,新{sortorder = ViewBag.EmployeeNameToVisitSort})

@ * In Time * @

@ Html.ActionLink(及时,排序,新{sortorder = ViewBag.InTimeSort})

< input type =submitid =btnCheckOutclass =btn btn-effect-ripple btn-primaryvalue =Check out>

通过



@ Html.DisplayFor(modelItem => item.OfficeLocation)

@ Html.DisplayFor(modelItem => item.Name)

@ Html.DisplayFor(modelItem => item.Mobile_number)

@ Html.DisplayFor(modelItem => item.EmployeeName_to_visit)

@ item.Office_in_time.Value.ToString(ddd,dd MMM, yyyy,HH:MM)

< label class =csscheckbox csscheckbox-primary>< input name = CheckOuttype =复选框id =@ ItemIdvalue =@ item.Visit_id> < / label>



I used this code

Controller:


public ActionResult Index(string sortorder)
{
visitorListBL = new VisitorListBL();
// List<visitordm> visitorDM = visitorListBL.GetCheckInVisitorList();
#region Created By Rajesh Mintri for sorting
var visitorDM = visitorListBL.GetCheckInVisitorList().AsEnumerable();
ViewBag.NameSort = String.IsNullOrEmpty(sortorder) ? "name_desc" : "";
ViewBag.InTimeSort = sortorder == "Date" ? "date_desc" : "Date";
ViewBag.OfficeLocationSort = sortorder == "OfficeLocation" ? "OfficeLocation_desc" : "OfficeLocation";
ViewBag.EmployeeNameToVisitSort = sortorder == "EmployeeNameToVisit" ? "EmployeeNameToVisit_desc" : "EmployeeNameToVisit";
ViewBag.MobileNumberSort = sortorder == "MobileNumber" ? "MobileNumber_desc" : "MobileNumber";
ViewBag.VisitIdSort = sortorder == "VisitId" ? "VisitId_desc" : "VisitId";
switch (sortorder)
{
case "name_desc":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.Name);
break;

case "Date":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderBy(objvisitor => objvisitor.Office_in_time);
break;
case "date_desc":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.Office_in_time);
break;
case "OfficeLocation":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderBy(objvisitor => objvisitor.OfficeLocation);
break;
case "OfficeLocation_desc":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.OfficeLocation);
break;
case "EmployeeNameToVisit":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderBy(objvisitor => objvisitor.EmployeeName_to_visit);
break;
case "EmployeeNameToVisit_desc":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.EmployeeName_to_visit);
break;
case "MobileNumber":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderBy(objvisitor => objvisitor.Mobile_number);
break;
case "MobileNumber_desc":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.Mobile_number);
break;
case "VisitId":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderBy(objvisitor => objvisitor.Visit_id);
break;
case "VisitId_desc":
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.Visit_id);
break;
default:
visitorDM = visitorListBL.GetCheckInVisitorList().OrderByDescending(objvisitor => objvisitor.Office_in_time);
break;
}
#endregion
return View(visitorDM);
}


view:

@foreach (var item in Model)
{
var ItemId = "VisitorChecKedOut-" + @item.Visit_id;
}

@*Visit Id*@
@Html.ActionLink("Visit Id", "Sorting", new { sortorder = ViewBag.VisitIdSort })

@*Office Location*@
@Html.ActionLink("Office Location", "Sorting", new { sortorder = ViewBag.OfficeLocationSort })
@*Visitor Name*@
@Html.ActionLink("Visitor Name", "Sorting", new { sortorder = ViewBag.NameSort })
@*Visitor Mobile Number*@
@Html.ActionLink("Visitor Mobile Number", "Sorting", new { sortorder = ViewBag.MobileNumberSort })
@*Person To Meet*@
@Html.ActionLink("Person To Meet", "Sorting", new { sortorder = ViewBag.EmployeeNameToVisitSort })
@*In Time*@
@Html.ActionLink("In Time", "Sorting", new { sortorder = ViewBag.InTimeSort })
<input type="submit" id="btnCheckOut" class="btn btn-effect-ripple btn-primary" value="Check Out">
Pass
@Html.DisplayFor(modelItem => item.Visit_id)
@Html.DisplayFor(modelItem => item.OfficeLocation)
@Html.DisplayFor(modelItem => item.Name)
@Html.DisplayFor(modelItem => item.Mobile_number)
@Html.DisplayFor(modelItem => item.EmployeeName_to_visit)
@item.Office_in_time.Value.ToString(" ddd, dd MMM, yyyy, HH:MM")
<label class="csscheckbox csscheckbox-primary"><input name="CheckOut" type="checkbox" id="@ItemId" value="@item.Visit_id" ></label>

推荐答案

<script src="/Scripts/jquery.tablesorter.js"></script>





然后初始化分拣机:





Then initialize the sorter:







然后将tablesorter类分配给要排序的表,并确保为标题行使用thead元素:





Then assign the tablesorter class to the table you want to sort and make sure you use a thead element for your header row:

<table class="tablesorter">
  <thead>
    <tr>
      <th>Name</th>
      <th>Date</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Joe Schmo</td>
      <td>08/26/2015</td>
      <td><a href="">Edit</a></td>
    </tr>
  </tbody>
</table>



这提供了一个非常简单的操作方法,但对于更多细节请转到我在开始时发布的网站链接。希望这会有所帮助,

Juan de Villiers


This provides a very simple how-to, but for more details go to the web site link I posted in the beginning. Hope this helps,
Juan de Villiers


这篇关于如何在不重新加载页面的情况下在MVC中进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 17:02