Confirmation box to delete record jquery flexigrid c#

248 Views Asked by At

i am populating jquery flexigrid by returning records in json format from c# controller. however i am facing little bit problem. I am adding herf column to delete the particular record. it works fine but i cant find way to confirm it before deleting it. below is my c# code which returns records to 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;
        }
    }

View

    $('#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
    });
1

There are 1 best solutions below

0
Koti Panga On

In CreateFlexiJson action, change below line

CurentsessionUser =="csagent"?"":String.Format("<a href=" + "'" + "Delete?subno=" + item.msisdn + "'" + ">Delete</a>")              

TO

CurentsessionUser =="csagent"?"":String.Format("<a href='javascript:void(0)' onclick='deleteSubscriber(\"" + item.msisdn + "\")'>Delete</a>")      

And add javascript function deleteSubscriber

function deleteSubscriber(subno) {
    if (confirm("Are you sure to delete Subscriber (No. = " + subno + ")")) {
        location.href = "Delete?subno=" + subno;
    }
}