i have implemented function delete record db table. if there error, want pass in viewbag. viewbag value not passed view.
controller
[httppost] public actionresult deleterecord(string name) { try { if (!(string.isnullorempty(name))) { driver_tab driver = db.driver_tab.singleordefault(m => m.name == name); db.driver_tab.remove(driver); db.savechanges(); viewbag.msg = "1"; messagebox.show(viewbag.msg); return view("main"); } else { viewbag.msg = "2"; return view("main"); } } catch (dbentityvalidationexception ex) { viewbag.msg = ex.message.tostring(); return view("main"); } catch (exception ex2) { viewbag.msg = ex2.message; return view("main"); } }
ajax function in view
function deleteselectedrecord() { var selectedname = $("#lstitems").val() $.ajax({ url: 'deleterecord', method: "post", data: { name: selectedname } }).done(function () { $("#lstitems").empty(); search(); getcount(); $('#modaldeleted').modal('show'); settimeout(function () { $('#modaldeleted').modal('hide'); }, 2000); }); }
i use viewbag.msg
in view <h2>@viewbag.msg
not working. problem in code.
if there error occurred in controller after executing delete function, want pass message view , display it. how accomplish that..?
you're ajax not using response deleterecord
. contains rendered view html, including viewbag data.
you need update javascript use response, looking @ code believe needs populate modal displaying:
something like:
$.ajax({ url: 'deleterecord', method: "post", data: { name: selectedname } }).done(function (data) { // response data, example: $('#modaldeleted').html(data); });
Comments
Post a Comment