简体   繁体   English

C#MVC:将网格搜索为部分视图以启用AJAX返回?

[英]C# MVC: Search Grid as Partial View to enable AJAX Return?

I have a grid that contains Contacts. 我有一个包含联系人的网格。 It automatically shows contacts when people go to the /contacts/ URL. 当人们访问/ contacts / URL时,它将自动显示联系人。 In a separate box, people can filter those by different criteria. 在一个单独的框中,人们可以按不同的条件过滤这些内容。 I want to refresh only the grid, not the entire page, whenever different criteria are applied. 每当应用不同的条件时,我只想刷新网格,而不刷新整个页面。

In order to achieve this, would I put the Contacts Grid by itself in a partial view, and somehow return the partial view via AJAX later? 为了实现这一点,我是否可以将Contacts Grid本身置于局部视图中,并稍后以某种方式通过AJAX返回局部视图?

Please give me a hint in the right direction. 请给我一个正确方向的提示。 Thank you:) 谢谢:)

The most straightforward method is probably to use the jQuery AJAX API to request the method. 最直接的方法可能是使用jQuery AJAX API来请求该方法。 You then use the Request.IsAjaxRequest() property in your controller to see if the request was made with AJAX - if so, you return only the PartialView from the Controller action. 然后,您可以在控制器中使用Request.IsAjaxRequest()属性来查看是否使用AJAX进行了请求-如果是这样,则仅从Controller操作中返回PartialView


Some code examples: 一些代码示例:

In your view, you could have the following markup: 在您看来,您可以具有以下标记:

<div id="gridContainer">
    <% Html.RenderPartial("ContactsGrid", ViewData.Model); %>
</div>

(assuming your contacts are contained in the Model object, and the view is strongly typed...) (假设您的联系人包含在Model对象中,并且视图的类型是强类型的...)
You then have the following javascript code called when you want to update the grid: 然后,当您要更新网格时,将调用以下javascript代码:

$('#gridContainer').load('/contacts/', { filterParameter: andItsValue });

You could also append the filter parameter in the URL, if your routes support that. 如果您的路由支持,也可以在URL中附加filter参数。

In your controller action, you do the following check before returning: 在控制器操作中,返回之前,请执行以下检查:

if(Request.IsAjaxRequest()) {
    return PartialView("ContactsGrid", contacts);
}
return View(contacts);

Something I put together a while ago... 我前几天整理的东西

I think this will get you started in the right direction. 我认为这将使您朝正确的方向开始。 It's a simple search people form using AJAX and a UserControl. 这是使用AJAX和UserControl的简单搜索人员表格。

我通常不喜欢仅发布链接,我喜欢编写快速示例,但是我认为这篇CodeProject文章的工作恰到好处: http : //www.codeproject.com/KB/aspnet/JQueryPartial.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM