调用Controller ActionResult来修改ViewBag变量并重新加载View?

时间:2015-04-14 15:30:14

标签: javascript jquery json asp.net-mvc viewbag

我有一个MVC5应用程序。应用程序加载中的 HomeController 填充了一个简单的列表,该列表通过 ViewBag 传递给索引视图,并将我的模型传递给View。在视图上我还有一个网格组件,其分页大小由 ViewBag 变量设置(在加载时,默认为10条记录):

    public ActionResult Index()
    {
        List<SelectListItem> pagingIntervalList = new List<SelectListItem>();
        pagingIntervalList.Add(new SelectListItem { Text = "5", Value = "5" });
        pagingIntervalList.Add(new SelectListItem { Text = "10", Value = "10" });
        pagingIntervalList.Add(new SelectListItem { Text = "25", Value = "25" });
        pagingIntervalList.Add(new SelectListItem { Text = "50", Value = "50" });
        pagingIntervalList.Add(new SelectListItem { Text = "100", Value = "100" });
        pagingIntervalList.Add(new SelectListItem { Text = "250", Value = "250" });
        pagingIntervalList.Add(new SelectListItem { Text = "500", Value = "500" });
        ViewBag.pagingIntervalList = pagingIntervalList;

        // Set default paging to 10 records on Load.
        ViewBag.PageSize = 10;

        var assetList = db.INV_ASSETS.ToList();
        return View(assetList);
    }

在此视图中,我尝试使用来自Controller的@Html.DropDownList()填充的SelectList,并允许用户指定他们希望分页发生的时间间隔(5, 10,25,50,100,250,500等):

<span class="gridPagerInterval label label-primary">Page every @Html.DropDownList("pagingIntervalList") Records. </span>

我无法弄清楚如何使用新选择的paging interval重新加载视图。在我的尝试中,我创建了Index()控制器操作的副本,该操作需要一个参数值,该参数值应该在ViewBag中使用并传递给View以设置新的分页记录选择:

public ActionResult NewPagingInterval(string selection)
        {
            List<SelectListItem> pagingIntervalList = new List<SelectListItem>();
            pagingIntervalList.Add(new SelectListItem { Text = "5", Value = "5" });
            pagingIntervalList.Add(new SelectListItem { Text = "10", Value = "10" });
            pagingIntervalList.Add(new SelectListItem { Text = "25", Value = "25" });
            pagingIntervalList.Add(new SelectListItem { Text = "50", Value = "50" });
            pagingIntervalList.Add(new SelectListItem { Text = "100", Value = "100" });
            pagingIntervalList.Add(new SelectListItem { Text = "250", Value = "250" });
            pagingIntervalList.Add(new SelectListItem { Text = "500", Value = "500" });
            ViewBag.pagingIntervalList = pagingIntervalList;

            // Set paging to user selected value.
            ViewBag.PageSize = selection;

            var assetList = db.INV_ASSETS.ToList();
            return View(assetList);
        }

我尝试使用下面的JSON调用来实现我想要的功能,但我需要重新加载View并使用新指定的ViewBag.PageSize值,而不仅仅是按原样重新加载:< / p>

       $("#pagingIntervalList").on("change", function (e) {
            var data = { selection: $("#pagingIntervalList").val() };
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: '@Url.Action("NewPagingInterval", "HOME")',
                data: data,
                success: function (resp) {
                    //alert("Success! Asset " + resp.ID + " successfully verified on " + resp.VDate);
                    location.reload(true);
                },
                error: function (resp) {
                    alert("There was an error with modifying the Paging interval...");
                }
            });
        });

然后我想也许我可以通过URL传递参数来调用我的NewPagingInterval(string selection)控制器动作,最后我把它放在一起:

$("#pagingIntervalList").on("change", function (e) {
    window.location = "/Home/NewPagingInterval?selection=" + $("#pagingIntervalList").val();
});

这成功调用了我在控制器上的NewPagingInterval(string selection)操作,但在return View(assetList)给了我之后:

Server Error in '/' Application.

The view 'NewPagingInterval' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/NewPagingInterval.aspx
~/Views/Home/NewPagingInterval.ascx
~/Views/Shared/NewPagingInterval.aspx
~/Views/Shared/NewPagingInterval.ascx
~/Views/Home/NewPagingInterval.cshtml
~/Views/Home/NewPagingInterval.vbhtml
~/Views/Shared/NewPagingInterval.cshtml
~/Views/Shared/NewPagingInterval.vbhtml
~/__MVCSITEMAPPROVIDER/NewPagingInterval.ascx

更有经验的人能否看到我出错的地方或有更好的想法来实现我的目标?


尝试haim的Git示例:

    // Initial Controller Action -- Loads Fine
    public ActionResult Index(int pageSize = 10)
    {
        List<SelectListItem> pagingIntervalList = new List<SelectListItem>();
        pagingIntervalList.Add(new SelectListItem { Text = "5", Value = "5" });
        pagingIntervalList.Add(new SelectListItem { Text = "10", Value = "10" });
        pagingIntervalList.Add(new SelectListItem { Text = "25", Value = "25" });
        pagingIntervalList.Add(new SelectListItem { Text = "50", Value = "50" });
        pagingIntervalList.Add(new SelectListItem { Text = "100", Value = "100" });
        pagingIntervalList.Add(new SelectListItem { Text = "250", Value = "250" });
        pagingIntervalList.Add(new SelectListItem { Text = "500", Value = "500" });
        ViewBag.pagingIntervalList = pagingIntervalList;

        // Set default paging to 10 records on Load.
        ViewBag.PageSize = pageSize;

        var assetList = db.INV_ASSETS.ToList();
        return View(assetList);
    }

// Form & DropDown
<form id="pagingIntervalForm" method="post" action="@Url.Action("NewPagingInterval", "HOME")">
    <span class="gridPagerInterval label label-primary">Page every @Html.DropDownList("pagingIntervalList") Records. </span>
</form> 

// Submit Form -- successfully calls NewPagingInterval Controller Action

// value of "selection" parameter is NULL!
$("#pagingIntervalList").on("change", function (e) {
      $('#pagingIntervalForm').submit();
});


    // Successfully called, but returns nothing but the raw data in text format of my Model.ToList().....?
    public ActionResult NewPagingInterval(string selection)
    {
        List<SelectListItem> pagingIntervalList = new List<SelectListItem>();
        pagingIntervalList.Add(new SelectListItem { Text = "5", Value = "5" });
        pagingIntervalList.Add(new SelectListItem { Text = "10", Value = "10" });
        pagingIntervalList.Add(new SelectListItem { Text = "25", Value = "25" });
        pagingIntervalList.Add(new SelectListItem { Text = "50", Value = "50" });
        pagingIntervalList.Add(new SelectListItem { Text = "100", Value = "100" });
        pagingIntervalList.Add(new SelectListItem { Text = "250", Value = "250" });
        pagingIntervalList.Add(new SelectListItem { Text = "500", Value = "500" });
        ViewBag.pagingIntervalList = pagingIntervalList;

        // Set paging to user selected value.
        ViewBag.PageSize = selection;

        var assetList = db.INV_ASSETS.ToList();
        return Json(assetList);
    }

1 个答案:

答案 0 :(得分:0)

在NewPagingInterval操作中尝试此操作:

ViewBag.pagingIntervalList = new SelectList(pagingIntervalList,“Value”,“Text”,selection);