1

我已经查看了其他解决方案,但我认为它们不适用于我的情况。

问题是一切运行良好并通过代码。当我调试时 - 所有值和一切都正确返回。但是当我查看网络时,它返回给我以下异常:“需要 jQuery 脚本引用才能在 \"WebGrid\" 帮助程序中启用 Ajax 支持。”。

这个想法是,一旦从下拉菜单中进行了选择,我就会尝试更新 webGrid 所属的部分视图。

所以控制器

    [HttpGet]
    public ActionResult FilterGrid(string selVal)
    {
        ItemType itemType = db.ItemTypes.FirstOrDefault(type => type.Name == selVal);

        IEnumerable<Item> items = db.Items.ToList().Where(types => types.ItemTypeId.ToString() == itemType.Id.ToString());

        return PartialView("_Grid", items.ToList());
    }

我将网格作为部分

<div id="grid">
    @Html.Partial("_Grid", items)
</div>

网格本身:

@using ManagerApp.Models
@model IEnumerable<Item>

@{ 
     var grid = new WebGrid(Model.ToList(), rowsPerPage: 5, ajaxUpdateContainerId:"grid");
    }
    @grid.GetHtml(columns: grid.Columns(
            grid.Column(columnName: "Name", header: "Name", format: (item) => @Ajax.ActionLink((string)item.Name, "ItemDetails", new { id = item.Id }, new AjaxOptions() { HttpMethod = "POST", UpdateTargetId="viewData", InsertionMode = InsertionMode.Replace }), style: "gridStyle"),
            grid.Column(columnName: "Price", header: "Price"),
            grid.Column("", format: (item) => @Ajax.ActionLink("Edit", "EditItem", new { id = item.Id }, new AjaxOptions() { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "viewData" })),
            grid.Column("", format: @<text>@Html.ActionLink("Delete", "DeleteItem", "ItemType", new { id = item.Id }, null)</text>)
 ))

和 ajax 脚本:

/// <reference path="jquery-1.5.1.js" />

$(function () {
    $("#GridItemTypes").change(function (event) {
        $.ajax({
            type: 'GET',
            url: "ItemType/FilterGrid",
            data: { selVal: $(this).find("option:selected").html() }
        });


    }
    );
});

所以它确实通过了所有的选择过滤,并进入局部视图,然后它似乎具有正确的值,但它没有呈现。

我在这里想念什么?

我在页面顶部有我的参考资料,因为将它们放在底部只会破坏我页面上的其他内容,而这个仍然无法正常工作。

我是否尝试发布或获取也没关系。

4

1 回答 1

0

您按什么顺序添加 javascript/jquery 引用?

确保 jquery 引用是最重要的。

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/other1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/other2.js")" type="text/javascript"></script>
于 2012-09-10T11:03:06.037 回答