如何更改获取数据的 WebGrid 操作 (.NET MVC3)
我有一个呈现 WebGrid 的部分视图。我的控制器看起来像
public ActionResult Index()
{
return View();
}
public ActionResult GetUserList(int? page, string sort, string sortdir)
{
var model = UserModel.getList(page,sort,sortdir);
return PartialView("_UserList",model);
}
Index.cshtml :
<代码>....
@Html.Action("GetUserList")
问题是,每次我单击网格导航或排序链接时,它都会调用 Index
方法。如何使 Webgrid 执行不同的操作(在本例中为 GetUserList
)?我确信我可以使用 jquery 将 GetUserList
添加到网格中的所有链接,但我相信这应该是更好的方法。
也有可能我所做的事情是完全错误的,所以感谢您的建议。
I have a Partial View that renders WebGrid. My controller looks like
public ActionResult Index()
{
return View();
}
public ActionResult GetUserList(int? page, string sort, string sortdir)
{
var model = UserModel.getList(page,sort,sortdir);
return PartialView("_UserList",model);
}
Index.cshtml :....
@Html.Action("GetUserList")
The problem is that every time I click on grid navigation or sort links it calls Index
method. How can I make Webgrid to execute a different action (GetUserList
in this case)? I'm sure I can prepend GetUserList
to all links in grid using jquery, but I believe it should be a better way.
It's also possible that what I'm doing is completely wrong, so thanks for your suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的结论不对。您只需将您的 webgrid 包装在 Get 表单中:
隐藏的内容是为了使排序目录和排序字段最终以可解析的形式出现在查询字符串中。你最终会得到像 localhost/ThingaMaBob/GetUserList?someotherfields=whatever=&sort=city&sortdir=ASC 这样的 url
Your conclusion isn't right. You just need to wrap your webgrid in a Get form:
The hiddens are so that the sort dir and sort field end up in parseable form in the querystring. You end up with urls like localhost/ThingaMaBob/GetUserList?someotherfields=whatever=&sort=city&sortdir=ASC
如果去掉[HttpPost]属性,让路由来到同样的函数。您将在方法中找到 Request["page"] 值。这将允许您检查 Request["Page"] 值。
If you remove [HttpPost] attribute and let the route come to the same function. you'll find the Request["page"] value in your method. this will allow you to put a check on Request["Page"] value.
经过大量的摸索和挖掘(甚至用 WebGrid 的源代码摆弄 Reflector),我得出的结论是,使用 WebGrid,您无法控制/更改标题链接操作。
要创建标头链接 URL,路径是从
HttpContext.Request.Path
获取的,因此无法将其自定义为指向不同的路由。一个非常丑陋的黑客是利用 jQuery Ajax 的事件(因为标题链接使用 jQuery.load 进行排序)并覆盖 URL:
更好的解决方案是使用:
After lot of monkeying around and digging (and even fiddling with Reflector with WebGrid's source code), I came to the conclusion that with WebGrid, you cannot control/change the Header link action.
To create the header link URL, the path is taken from
HttpContext.Request.Path
, so there is no way to customize it to point to a different route.One very ugly hack would be to tap into to jQuery Ajax's events (since the header link uses jQuery.load to sort) and overwrite the URL:
Better solution would be to use:
@MrChief 有上面关于丑陋黑客的想法......我把它们放在一起。这是我用来执行此操作的主要代码。事实上,它确实在 ajax 调用上线之前就对其进行了劫持。关键是修改正在发送的 URL,因为网格将从 HttpContext.Request.Path 获取该 URL。并将其插入到锚元素的 onclick 中。
我将其放入主 common.js 中,并简单地附加一个函数来捕获在发送数据之前发生的 ajaxSend 事件。
渲染页面时,我在列标题中标记了包含不正确 URL 的标签,其中包含名为“redosorturl”的类,因此我知道当我劫持 ajax 调用时,必须在此元素上完成操作。然后我调用自定义函数为我提供正确的 URL,然后使用该新 URL 重写 ajaxOptions.url,
我必须将 activeElement 传递给该重写函数,以便我可以遍历 DOM 以获取我已放置数据的网格信息。就像与我用于 URL 的 ID 和其他信息一起使用的控制器和操作方法一样,我传入当前的 url 字符串,因为网格将在我解析的 url 末尾注入一个令牌,并且添加新的网址。
@MrChief had the idea above about the ugly hack...I put that together. Here is the main code that I used to do this. It does, indeed, hijack the ajax call before it is put on the wire. The key is to modify the URL that is getting sent because the grid will grab that URL from HttpContext.Request.Path. and plug it into the onclick for the anchor element.
I put this into my main common.js and will simply attach a function to capture the ajaxSend event which happens just before the data is sent.
When rendering the page, I have marked the tag in column header that contains the incorrect URL with a class named "redosorturl', so I know when I hijack the ajax call, the operation has to be done on this element. I then call a custom function that gives me the correct URL, then the ajaxOptions.url is then rewritten with that new URL.
I have to pass the activeElement to that rewrite function so I can traverse up the DOM to get the grid information, where I have put data like the controller and action method that is used along with and IDs and other info that I use for the URL. Likewise, I pass in the current url string because the grid will inject a token at the end of the url that I parse off and put on the new url.