I am facing an issue where I need to select a value from a grid and pass it to ajax, but I keep getting an error saying "Id undefined". Can anyone help me find a solution for this problem? The goal is to delete records that are presented in the grid. Upon clicking the button, the selected value should be passed to an ajax function for the deletion process. While the value is successfully moved to ajax, the error occurs stating that 'policyid' is undefined. Thank you in advance for any assistance.
Code snippet for the grid column:
@{
var grid = new WebGrid(Model.Policy, rowsPerPage: 20, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridcal");
grid.Pager(WebGridPagerModes.NextPrevious);}
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns:
grid.Columns(
grid.Column("PolicyName","Policy Name",style: "colWidth"),
grid.Column("PolicyValue","Policy Value",style: "colWidth"),
// grid.Column ("Delete",format:@<text><a href="@Url.Action("DeletePolicy","Roles",new{PolicyId=item.PolicyId.ToString()})"><img src="~/Images/img-delete-blk-icon.png" width="9" height="9" alt="Delete"/></a> </text>)
****grid.Column(format: @<text><input type="image" onclick="AJAXCall_Fun_DeletePolicy()" src="~/Images/img-delete-blk-icon.png" name="image" width="9" height="9" /></text>)****
))
@if (grid.HasSelection)
{
<b>Policy Name</b>@AppliedPolicies.PolicyName<br />
<b>Policy Value</b>@AppliedPolicies.PolicyValue<br />
}
Ajax function:
function AJAXCall_Fun_DeletePolicy() {
if ($.xhrPool.length > 0) {
$.each($.xhrPool, function (idx, jqXHR) {
if (jqXHR) {
this.abort();
}
});
$.xhrPool = [];
}
var PolicyId = getPolicyId();
if (PolicyId.length > 0) {
$.ajax({
type: "GET",
url: "/Roles/DeletePolicy",
data: { 'PolicyId': JSON.stringify(PolicyId) },
async: true,
cache: false,
datatype: "json",
Controller code snippet:
public JsonResult DeletePolicy(string PolicyId)
{
bool status = false;
using (clsBLLGroups objclsBLLGroups = new clsBLLGroups())
{
status = objclsBLLGroups.DeletePolicy(UserCookieWrapper.UserAccessToken, PolicyId.ToString());
}
return Json(status, JsonRequestBehavior.AllowGet);
}