简体   繁体   English

将值从javascript传递到mvc4中的ajax

[英]passing value from javascript to ajax in mvc4

i need to select a value from a grid and pass it to ajax. 我需要从网格中选择一个值,并将其传递给ajax。 I'm getting the error as "Id undefined". 我收到的错误信息为“ Id undefined”。 can anyone suggest me a solution for this issue. 谁能建议我解决这个问题的方法。 Records most be deleted which a presented in grid. 记录在网格中显示的最多删除记录。 on clicking the button selected value must got to ajax function for deletion process. 单击按钮后,所选值必须进入ajax函数才能进行删除过程。 value s moved to ajax but getting error that the policyid is undefined Thanks in advance. 值s移至ajax,但收到未定义policyid的错误,提前致谢。

Code of 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 AJAXCall_Fun_DeletePolicy() {

    if ($.xhrPool.length > 0) {
        $.each($.xhrPool, function (idx, jqXHR) {
            if (jqXHR) {
                this.abort();
            }
        });
        $.xhrPool = [];
    }


  var PolicyId = PolicyId();
    if (PolicyId.length > 0) {
        $.ajax({

            type: "GET",
            url: "/Roles/DeletePolicy",
            data: { 'PolicyId': JSON.stringify(PolicyId) },
            async: true,
            cache: false,
            datatype: "json",

Controller code: 控制器代码:

    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);
    }

a web grid renders as a table so use Get id of selected row in a table -HTML to get the id of clicked row and you can pass that through your ajax call to the controller. Web网格呈现为表格,因此请使用获取表格中所选行的ID -HTML来获取被单击的行的ID,您可以通过ajax调用将其传递给控制器​​。
You mentioned PolicyId is controller code. 您提到PolicyId是控制器代码。 If you want to include a value from your model in your ajax call you just need to use @ 如果要在ajax调用中包含模型中的值,则只需使用@

var PolicyId = '@(Model.PolicyId)';

you can use that for your url as well. 您也可以将其用于您的网址。 We use 我们用

url: '@Url.Action("DeletePolicy", "Roles")',

for our url. 为我们的网址。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM