我通过从c#controller返回json格式的记录来填充jquery flexigrid。但是我面临一点点问题。我正在添加herf列以删除特定记录。它工作正常但我无法找到方法来确认它删除它之前。下面是我的c#代码,它将记录返回给flexigrid。
C#Controller Snippet
private JsonResult CreateFlexiJson(IEnumerable<user> items, int page, int total)
{
var CurentsessionUser = Session["sessionUserId"].ToString();
List<Object> rows = new List<Object>();
foreach (var item in items)
{
rows.Add(new
{
id = item.id,
cell = new string[] {
item.msisdn,
item.pin,
item.subtype,
CurentsessionUser =="csagent"?"":String.Format("<a href=" + "'" + "ChangePin?subno=" + item.msisdn + "'" + ">Change Pin</a>"),
CurentsessionUser =="csagent"?"":String.Format("<a href=" + "'" + "Delete?subno=" + item.msisdn + "'" + ">Delete</a>")
}
});
}
var result = new { page = page, total = total, rows = rows };
return Json(result);
}
public ActionResult Delete(string subno)
{
try
{
wmas_subsEntities entitymodel = new wmas_subsEntities();
var customer = from p in entitymodel.users where p.msisdn == subno select p;
if (customer.ToList().Count > 0)
{
entitymodel.users.Remove(customer.First());
entitymodel.SaveChanges();
}
//return Json("Successfully deleted the user registeration");
return View("Index");
}
catch (Exception ex)
{
throw ex;
}
}
查看
$('#CustomerList').flexigrid({
dataType: 'json',
colModel: [
{
display: 'Subscriber No.',
name: 'msisdn',
width: 100,
sortable: true,
align: 'left'
},
{
display: 'Pin Code',
name: 'pin',
width: 100,
sortable: true,
align: 'left'
},
{
display: 'Postpaid/Prepaid',
name: 'subtype',
width: 100,
sortable: true,
align: 'left'
},
{
display: '',
name: '',
width: 100,
sortable: true,
align: 'left'
},
{
display: '',
name: '',
width: 100,
sortable: true,
align: 'left'
}
],
title: 'Customer',
useRp: true,
rp: 15,
width: 600,
height:400,
singleSelect: true
});
答案 0 :(得分:0)
在CreateFlexiJson
操作中,更改以下行
CurentsessionUser =="csagent"?"":String.Format("<a href=" + "'" + "Delete?subno=" + item.msisdn + "'" + ">Delete</a>")
要
CurentsessionUser =="csagent"?"":String.Format("<a href='javascript:void(0)' onclick='deleteSubscriber(\"" + item.msisdn + "\")'>Delete</a>")
并添加javascript function deleteSubscriber
function deleteSubscriber(subno) {
if (confirm("Are you sure to delete Subscriber (No. = " + subno + ")")) {
location.href = "Delete?subno=" + subno;
}
}