【问题标题】:ASP.NET MVC 3 WebGrid, preserve the paging and define the routeASP.NET MVC 3 WebGrid,保留分页并定义路由
【发布时间】:2012-02-10 03:28:00
【问题描述】:

我开始学习MVC3,但是我在WebGrid上遇到了一些问题,

控制器名称是 TestController,所以我导航到: http://localhost:53503/Test/

Webgrid如下图:

两列:ID 和名称。 第 3 和第 4 列:Actionlink 和 item.GetSelectLink。

第一个问题是: 假设我正在查看webgrid的第3页,并按下ActionLink,在考虑TestContoller的Action后,webgrid会在PostBack后返回到第1页。

但是,如果我按下右端的 [View(GetSelectLink)]:
(例如http://localhost:53503/Test/?gridItems_page=3&gridItems_selectedRow=3
它有效。

那么,我应该使用 ActionLink 或 GetSelectLink 进行一般的添加/更新/删除操作吗?

MVC3 没有 viewstate/control 状态,如何在 PostBack 后保留当前页面选择?

第二个问题是: ActionLink 的 (href) 是:
http://localhost:53503/Test/GetSelection/7?Name=PSP
我会到
http://localhost:53503/Test/GetSelection/7/PSP

我向 global.asax 添加了一条新路由,但运气不好。

非常感谢您的帮助。

这是我的代码:

Views/Test/Index.cshtml

@model List<MvcContract.Controllers.Products>

@{
    System.Web.Helpers.WebGrid grid = new System.Web.Helpers.WebGrid(
        source: Model,
        canPage: true,
        rowsPerPage: 3,
        fieldNamePrefix: "gridItems_",
        pageFieldName: "page",
        selectionFieldName: "selectedRow"
       );
}

@{      
    if (Model != null)
    {
    @grid.GetHtml(
                columns: grid.Columns(
                grid.Column("ID"),
                grid.Column("Name"),
                        grid.Column(format: (item) => Html.ActionLink("View(ActionLink)", "GetSelection", new { ID = item.ID, Name = item.Name })),
                        grid.Column(format: (item) => item.GetSelectLink("View(GetSelectLink)"))
               )
               );
     }
}

Controllers/TestController.cs

namespace MvcContract.Controllers
{
    public class Products
    {
        public string ID { get; set; }
        public string Name { get; set; }

        public List<Products> GetItems()
        {
            List<Products> items = new List<Products>(); 
            items.Add(new Products() { ID = "1", Name = "PS3" });
            items.Add(new Products() { ID = "2", Name = "XBox360" });
            items.Add(new Products() { ID = "3", Name = "Wii" });
            items.Add(new Products() { ID = "4", Name = "Saturn" });
            items.Add(new Products() { ID = "5", Name = "Dreamcast" });
            items.Add(new Products() { ID = "6", Name = "NDS" });
            items.Add(new Products() { ID = "7", Name = "PSP" });
            items.Add(new Products() { ID = "8", Name = "NeoGeo" });
            items.Add(new Products() { ID = "9", Name = "3DO" });
            items.Add(new Products() { ID = "10", Name = "Playdia" });
            return items;
        }
    }

    public class TestController : Controller
    {
        //Bind data to WebGrid
        public ActionResult Index()
        {
            Products products = new Products();
            return View(products.GetItems());
        }

        //Some Logic
        public ActionResult GetSelection(string ID, string Name)
        {
            string SelectedID = ID;

            return RedirectToAction("Index");
        }

    }
}

在 Global.asax.cs 中注册路由()

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

    routes.MapRoute(
        "Test_GetSelection", // Route name
        "{controller}/{action}/{id}/{name}", // URL with parameters
        new { controller = "Test", action = "GetSelection", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
    );
}

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    查看this discussion。我认为那里的几种解决方案之一可能会解决您的问题。

    自从您发布此消息以来已经整整一年,如果您以其他方式找到答案,请发布答案。

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 2011-05-10
      • 2013-02-15
      • 2012-06-21
      • 1970-01-01
      • 2011-08-11
      • 2013-09-23
      • 1970-01-01
      相关资源
      最近更新 更多