Javascript MVC中的服务器端列呈现问题
我是Mvc Jquery datatable服务器端操作的新手,我在使用Jquery datatable呈现列表以查看时遇到一个问题,即在我的表中,我有6列,第6列有多个@Html按钮,其值基于第5列 例如:如果第5列的值为“Register”,则我必须显示编辑、删除、批准按钮等,如果其为“Deactive”,则我必须显示编辑、删除、反应按钮 如果我禁用了服务器端功能,它的工作就像一个符咒,但是当我创建一个ajax处理程序方法并以json格式发送请求时,它会显示空字符串,我知道我必须为“data”列中的第6列编写函数,但我不知道如何处理它 index.htmlJavascript MVC中的服务器端列呈现问题,javascript,jquery,asp.net-mvc,datatable,Javascript,Jquery,Asp.net Mvc,Datatable,我是Mvc Jquery datatable服务器端操作的新手,我在使用Jquery datatable呈现列表以查看时遇到一个问题,即在我的表中,我有6列,第6列有多个@Html按钮,其值基于第5列 例如:如果第5列的值为“Register”,则我必须显示编辑、删除、批准按钮等,如果其为“Deactive”,则我必须显示编辑、删除、反应按钮 如果我禁用了服务器端功能,它的工作就像一个符咒,但是当我创建一个ajax处理程序方法并以json格式发送请求时,它会显示空字符串,我知道我必须为“data
<table id="TblUsersAccounts" class="table">
<thead>
<tr>
<th>ID</th>
<th>Group</th>
<th>FullName</th>
<th>Email</th>
<th>Role</th>
<th>AccountStatus</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr id="@item.Id" onclick="rowFunction(this)">
<td>@item.Id</td>
<td>@item.Group</td>
<td>@item.FullName</td>
<td>@item.Email</td>
<td>@item.Role</td>
<td id="@item.AccountStatus" class="AcntStatus">@item.AccountStatus</td>
<td>
<a href="/ManageUserAccount/EditUserAccount"><span class="btn btn-sm btn-primary">Edit</span></a>
@if (item.AccountStatus.Trim() == string.Empty)
{
<a href="#"><span class="btn btn-sm btn-primary">Resend</span></a>
@Ajax.ActionLink("Delete", "DeleteUser", new { id = item.Id }, new AjaxOptions() { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, OnBegin = "DeleteCalls(this)", UpdateTargetId = "modalGenericContent", OnComplete = "OpenModal(this)" }, new { id = "deleteUser", @class = "btn btn-sm btn-primary", @text = "Delete User Accounts", name = item.Id })
}
else if (item.AccountStatus.Trim() == "Registered")
{
<a href="#"><span class="btn btn-sm btn-primary">Deactivate</span></a>
}
else if (item.AccountStatus.Trim() == "None")
{
<a href="#"><span class="btn btn-sm btn-primary">Resend</span></a>
@Ajax.ActionLink("Delete", "DeleteUser", new { id = item.Id }, new AjaxOptions() { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, OnBegin = "DeleteCalls(this)", UpdateTargetId = "modalGenericContent", OnComplete = "OpenModal(this)" }, new { id = "deleteUser", @class = "btn btn-sm btn-primary", @text = "Delete User Accounts", name = item.Id })
}
else if (item.AccountStatus.Trim() == "Deactivated")
{
<a href="#"><span class="btn btn-sm btn-primary">Re Activate</span></a>
}
</td>
</tr>
}
</tbody>
身份证件
组
全名
电子邮件
角色
帐户状态
行动
@foreach(模型中的var项目)
{
@项目Id
@项目组
@item.FullName
@邮件
@项目.角色
@项目.帐户状态
@if(item.AccountStatus.Trim()==string.Empty)
{
@ActionLink(“Delete”、“DeleteUser”、new{id=item.id}、new AjaxOptions(){HttpMethod=“GET”、InsertionMode=InsertionMode.Replace、OnBegin=“deleteCalles(this)”、UpdateTargetId=“modalGenericContent”、OnComplete=“openmodel(this)”、new{{id=“DeleteUser”、@class=“btn btn sm btn primary”、@text=“删除用户帐户”,name=item.Id})
}
else if(item.AccountStatus.Trim()=“已注册”)
{
}
else if(item.AccountStatus.Trim()=“无”)
{
@ActionLink(“Delete”、“DeleteUser”、new{id=item.id}、new AjaxOptions(){HttpMethod=“GET”、InsertionMode=InsertionMode.Replace、OnBegin=“deleteCalles(this)”、UpdateTargetId=“modalGenericContent”、OnComplete=“openmodel(this)”、new{{id=“DeleteUser”、@class=“btn btn sm btn primary”、@text=“删除用户帐户”,name=item.Id})
}
else if(item.AccountStatus.Trim()=“已停用”)
{
}
}
Index.js
$(document).ready(function () {
var cells = $('td.AcntStatus');
var table = $('#TblUsersAccounts').DataTable({
"serverSide": true,
"ajax": {
"type": "POST",
"url": '/ManageUserAccount/DataHandler',
"contentType": 'application/json; charset=utf-8',
'data': function (data) { return data = JSON.stringify(data); }
},
paging: true,
ordering: true,
info: false,
searching: true,
bLengthChange: false,
"processing": true,
//"paging": true,
"deferRender": true,
"columns": [
{ "data": "Id" },
{ "data": "FullName" },
{ "data": "Group" },
{ "data": "Email" },
{ "data": "Role" },
{ "data": "AccountStatus" },
{
//"class": 'details-control',
"orderable": false,
"data": "Action",
"defaultcontent": "<button>Click!</button>", // I have to render the buttons for Action
"searchable": false
// added this to disable search on first column
}
],
"order": [0, "asc"]
});
public ActionResult Index()
{
List<UserAccountViewModel> userAccountLst = GetList();
return View(userAccountLst);// return }
}
public List<UserAccountViewModel> GetList()
{
UserAccountViewModel useraccountviewmodel = new UserAccountViewModel();
List<UserAccountViewModel> userAccountLst = new List<UserAccountViewModel>();
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC1", FirstName = "Principal1", LastName = "Admin", Email = "admin@quad1.in", Group = "GroupA", Role = "Administrator", AccountStatus = "None" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC2", FirstName = "Principal2", LastName = "Admin", Email = "Student@quad2.in", Group = "GroupB", Role = "Student", AccountStatus = "Deactivated" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC3", FirstName = "Principal3", LastName = "Admin", Email = "admin@quad3.in", Group = "GroupC", Role = "Administrator", AccountStatus = "Registered" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC4", FirstName = "Principal4", LastName = "Admin", Email = "admin@quad4.in", Group = "GroupD", Role = "Administrator", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC5", FirstName = "Principal5", LastName = "Admin", Email = "Student@quad5.in", Group = "GroupE", Role = "Student", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC6", FirstName = "Principal6", LastName = "Admin", Email = "Advisor@quad6.in", Group = "GroupF", Role = "Advisor", AccountStatus = "None" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC7", FirstName = "Principal7", LastName = "Admin", Email = "Student@quad7.in", Group = "GroupA", Role = "Student", AccountStatus = "Deactivated" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC8", FirstName = "Principal8", LastName = "Admin", Email = "admin@quad8.in", Group = "GroupA", Role = "Administrator", AccountStatus = "Registered" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC9", FirstName = "Principal9", LastName = "Admin", Email = "admin@quad9.in", Group = "GroupC", Role = "Administrator", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC10", FirstName = "Principal10", LastName = "Admin", Email = "Student@quad10.in", Group = "GroupA", Role = "Student", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC11", FirstName = "Principal11", LastName = "Admin", Email = "admin@quad11.in", Group = "GroupF", Role = "Administrator", AccountStatus = "None" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC12", FirstName = "Principal12", LastName = "Admin", Email = "Advisor@quad12.in", Group = "GroupA", Role = "Advisor", AccountStatus = "Deactivated" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC13", FirstName = "Principal13", LastName = "Admin", Email = "admin@quad13.in", Group = "GroupE", Role = "Administrator", AccountStatus = "Registered" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC14", FirstName = "Principal14", LastName = "Admin", Email = "Advisor@quad14.in", Group = "GroupD", Role = "Advisor", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC15", FirstName = "Principal15", LastName = "Admin", Email = "admin@quad15.in", Group = "GroupB", Role = "Administrator", AccountStatus = string.Empty });
useraccountviewmodel.UserAccountlst = userAccountLst;
return userAccountLst;
}
public JsonResult DataHandler(DTParameters param)
{
try
{
List<UserAccountViewModel> dtsource;
dtsource =GetList().ToList();
List<String> columnSearch = new List<string>();
foreach (var col in param.Columns)
{
columnSearch.Add(col.Search.Value);
}
List<UserAccountViewModel> data = new ResultSet().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, dtsource, columnSearch);
int count = new ResultSet().Count(param.Search.Value, dtsource, columnSearch);
DTResult<UserAccountViewModel> result = new DTResult<UserAccountViewModel>
{
draw = param.Draw,
data = data,
recordsFiltered = count,
recordsTotal = count
};
return Json(result);
}
catch (Exception ex)
{
return Json(new { error = ex.Message });
}
}
$(文档).ready(函数(){
变量单元格=$('td.AcntStatus');
变量表=$('#TBluserAccounts')。数据表({
“服务器端”:正确,
“ajax”:{
“类型”:“职位”,
“url”:“/ManageUserAccount/DataHandler”,
“contentType”:“application/json;charset=utf-8”,
“数据”:函数(数据){return data=JSON.stringify(数据);}
},
是的,
顺序:对,
信息:错,
搜索:是的,
bLengthChange:false,
“处理”:对,
//“分页”:正确,
“延迟渲染”:正确,
“栏目”:[
{“数据”:“Id”},
{“数据”:“全名”},
{“数据”:“组”},
{“数据”:“电子邮件”},
{“数据”:“角色”},
{“数据”:“帐户状态”},
{
//“类”:“详细信息控制”,
“可订购”:错误,
“数据”:“行动”,
“defaultcontent”:“单击!”,//我必须呈现按钮以执行操作
“可搜索”:false
//添加此选项以禁用对第一列的搜索
}
],
“订单”:[0,“asc”]
});
截图:
AccountsController.cs
$(document).ready(function () {
var cells = $('td.AcntStatus');
var table = $('#TblUsersAccounts').DataTable({
"serverSide": true,
"ajax": {
"type": "POST",
"url": '/ManageUserAccount/DataHandler',
"contentType": 'application/json; charset=utf-8',
'data': function (data) { return data = JSON.stringify(data); }
},
paging: true,
ordering: true,
info: false,
searching: true,
bLengthChange: false,
"processing": true,
//"paging": true,
"deferRender": true,
"columns": [
{ "data": "Id" },
{ "data": "FullName" },
{ "data": "Group" },
{ "data": "Email" },
{ "data": "Role" },
{ "data": "AccountStatus" },
{
//"class": 'details-control',
"orderable": false,
"data": "Action",
"defaultcontent": "<button>Click!</button>", // I have to render the buttons for Action
"searchable": false
// added this to disable search on first column
}
],
"order": [0, "asc"]
});
public ActionResult Index()
{
List<UserAccountViewModel> userAccountLst = GetList();
return View(userAccountLst);// return }
}
public List<UserAccountViewModel> GetList()
{
UserAccountViewModel useraccountviewmodel = new UserAccountViewModel();
List<UserAccountViewModel> userAccountLst = new List<UserAccountViewModel>();
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC1", FirstName = "Principal1", LastName = "Admin", Email = "admin@quad1.in", Group = "GroupA", Role = "Administrator", AccountStatus = "None" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC2", FirstName = "Principal2", LastName = "Admin", Email = "Student@quad2.in", Group = "GroupB", Role = "Student", AccountStatus = "Deactivated" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC3", FirstName = "Principal3", LastName = "Admin", Email = "admin@quad3.in", Group = "GroupC", Role = "Administrator", AccountStatus = "Registered" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC4", FirstName = "Principal4", LastName = "Admin", Email = "admin@quad4.in", Group = "GroupD", Role = "Administrator", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC5", FirstName = "Principal5", LastName = "Admin", Email = "Student@quad5.in", Group = "GroupE", Role = "Student", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC6", FirstName = "Principal6", LastName = "Admin", Email = "Advisor@quad6.in", Group = "GroupF", Role = "Advisor", AccountStatus = "None" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC7", FirstName = "Principal7", LastName = "Admin", Email = "Student@quad7.in", Group = "GroupA", Role = "Student", AccountStatus = "Deactivated" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC8", FirstName = "Principal8", LastName = "Admin", Email = "admin@quad8.in", Group = "GroupA", Role = "Administrator", AccountStatus = "Registered" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC9", FirstName = "Principal9", LastName = "Admin", Email = "admin@quad9.in", Group = "GroupC", Role = "Administrator", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC10", FirstName = "Principal10", LastName = "Admin", Email = "Student@quad10.in", Group = "GroupA", Role = "Student", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC11", FirstName = "Principal11", LastName = "Admin", Email = "admin@quad11.in", Group = "GroupF", Role = "Administrator", AccountStatus = "None" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC12", FirstName = "Principal12", LastName = "Admin", Email = "Advisor@quad12.in", Group = "GroupA", Role = "Advisor", AccountStatus = "Deactivated" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC13", FirstName = "Principal13", LastName = "Admin", Email = "admin@quad13.in", Group = "GroupE", Role = "Administrator", AccountStatus = "Registered" });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC14", FirstName = "Principal14", LastName = "Admin", Email = "Advisor@quad14.in", Group = "GroupD", Role = "Advisor", AccountStatus = string.Empty });
userAccountLst.Add(new UserAccountViewModel { Id = "OSDC15", FirstName = "Principal15", LastName = "Admin", Email = "admin@quad15.in", Group = "GroupB", Role = "Administrator", AccountStatus = string.Empty });
useraccountviewmodel.UserAccountlst = userAccountLst;
return userAccountLst;
}
public JsonResult DataHandler(DTParameters param)
{
try
{
List<UserAccountViewModel> dtsource;
dtsource =GetList().ToList();
List<String> columnSearch = new List<string>();
foreach (var col in param.Columns)
{
columnSearch.Add(col.Search.Value);
}
List<UserAccountViewModel> data = new ResultSet().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, dtsource, columnSearch);
int count = new ResultSet().Count(param.Search.Value, dtsource, columnSearch);
DTResult<UserAccountViewModel> result = new DTResult<UserAccountViewModel>
{
draw = param.Draw,
data = data,
recordsFiltered = count,
recordsTotal = count
};
return Json(result);
}
catch (Exception ex)
{
return Json(new { error = ex.Message });
}
}
public ActionResult Index()
{
List userAccountLst=GetList();
返回视图(userAccountLst);//返回}
}
公共列表GetList()
{
UserAccountViewModel UserAccountViewModel=新的UserAccountViewModel();
List userAccountLst=新列表();
添加(新的UserAccountViewModel{Id=“OSDC1”,FirstName=“Principal1”,LastName=“Admin”,Email=”admin@quad1.in,Group=“GroupA”,Role=“Administrator”,AccountStatus=“None”});
添加(新的UserAccountViewModel{Id=“OSDC2”,FirstName=“Principal2”,LastName=“Admin”,Email=”Student@quad2.in“,Group=“GroupB”,Role=“Student”,AccountStatus=“Deactivated”});
添加(新的UserAccountViewModel{Id=“OSDC3”,FirstName=“Principal3”,LastName=“Admin”,Email=”admin@quad3.in“,Group=“GroupC”,Role=“Administrator”,AccountStatus=“Registered”});
添加(新的UserAccountViewModel{Id=“OSDC4”,FirstName=“Principal4”,LastName=“Admin”,Email=”admin@quad4.in,Group=“GroupD”,Role=“Administrator”,AccountStatus=string.Empty});
添加(新的UserAccountViewModel{Id=“OSDC5”,FirstName=“Principal5”,LastName=“Admin”,Email=”Student@quad5.in,Group=“GroupE”,Role=“Student”,AccountStatus=string.Empty});
添加(新的UserAccountViewModel{Id=“OSDC6”,FirstName=“Principal6”,LastName=“Admin”,Email=”Advisor@quad6.in,Group=“GroupF”,Role=“Advisor”,AccountStatus=“None”});
添加(新的UserAccountViewModel{Id=“OSDC7”,FirstName=“Principal7”,LastName=“Admin”,Email=”Student@quad7.in,Group=“GroupA”,Role=“Student”,AccountStatus=“Deactivated”});
添加(新的UserAccountViewModel{Id=“OSDC8”,FirstName=“Principal8”,LastName=“Admin”,Email=”admin@quad8.in,Group=“GroupA”,Role=“Administrator”,AccountStatus=“Registered”});
userAccountLst.Add(新的UserAcc