0

这是我用 onclick 函数()标记的剑道网格<href>。现在我想将文件名作为参数传递给我的 onclick 函数。我想将该文件名作为参数传递给 CallAjaxMethod 函数。怎么办?

@(Html.Kendo().Grid<MVCAPPModels..AttachmentsModel>()
        .Name("grid")
        .Columns(columns =>
        {   
            columns.Bound(p => p.FileName).ClientTemplate("<a id='FileName' href='\\#' data-pdfurl='#= FileName #' onclick='CallAjaxMethod('#= FileName #');'>/#= FileName #</a>");

            columns.Bound(c => c.CreatedDate).Width(70);
            columns.Bound(c => c.CreatedBy).Width(70);
            columns.Bound(c => c.UpdatedDate).Width(70);
            columns.Bound(c => c.UpdatedbBy).Width(70);
        })

        .HtmlAttributes(new { style = "height: 350px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(1))
        .DataSource(dataSource => dataSource
            .Ajax()
                      .ServerOperation(false)

            .Read(read => read.Action("Customers_Read", "Home"))
        )
)

JavaScript 函数:

function CallAjaxMethod()
    {
        alert("page! CallAjax");
        $.ajax({
            type: "POST",
            url: "@Url.Action("Submit", "Home")",
            data: { fileName: "/pdfSample.pdf" },
            type: 'GET',
            cache: false,
            success: function (result) {
                        $('#partialView_div').data(result);
            }
        });
    }
4

1 回答 1

0

首先,为你的函数添加一个参数,这样你就可以实际使用传入的内容:

function CallAjaxMethod(filename)
{
    alert("page! CallAjax");
    $.ajax({
        type: "POST",
        url: "@Url.Action("Submit", "Home")",
        data: { fileName: filename },
        type: 'GET',
        cache: false,
        success: function (result) {
                    $('#partialView_div').data(result);
        }
    });
}

然后您的 HTML 和 Javascript 引号相互冲突,因为它们都是单引号,因此您需要更改其中的一些,最好是 HTML 的:

@(Html.Kendo().Grid<MVCAPPModels.AttachmentsModel>()
        .Name("grid")
        .Columns(columns =>
        {   
            columns.Bound(p => p.FileName).ClientTemplate("<a id=\"FileName\" href=\"\\#\" data-pdfurl=\"#= FileName #\" onclick=\"CallAjaxMethod('#= FileName #');\">/#= FileName #</a>");

            columns.Bound(c => c.CreatedDate).Width(70);
            columns.Bound(c => c.CreatedBy).Width(70);
            columns.Bound(c => c.UpdatedDate).Width(70);
            columns.Bound(c => c.UpdatedbBy).Width(70);
        })

        .HtmlAttributes(new { style = "height: 350px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(1))
        .DataSource(dataSource => dataSource
            .Ajax()
                      .ServerOperation(false)

            .Read(read => read.Action("Customers_Read", "Home"))
        )
)
于 2014-12-04T09:31:35.743 回答