如何使用 jQuery 过滤 MVC 部分视图中的数据

发布于 2024-07-30 02:00:14 字数 2820 浏览 3 评论 0原文

当我更改 Index.aspx 页面上的下拉列表时,如何使用 Ajax、jQuery 更新数据? 我有一个带有项目列表下拉列表的页面,其中应该显示与该项目相关的所有问题。

如果我更改控制器操作的返回值,请参阅注释代码,它要么删除母版页并仅加载 PartialView 要么不加载任何内容。 我也尝试做类似的事情: return PartialView("MyIssues", paginationIssues);

通过下面的代码,我可以使用 Ajax 更改页面,但无法返回正确的数据或 html 来刷新 PartialView。

那么我做错了什么或者如何完成数据刷新?

Index.aspx

<label for="ProjectList">Project:</label>
<%= Html.DropDownList("ProjectList", "--All--") %>
<div id="divMyIssues"><% Html.RenderPartial("MyIssues", Model); %></div>
<script type="text/javascript">
$(document).ready(function() {
     $("#ProjectList").change(function() {
         CanIRefresh();
     });
  });
  function CanIRefresh() {
     $.ajax({
         type: "POST",
         url: "/Issue/" + "Index",
         dataType: "html",
         data: {
             page: 5// just changing the page to see if it updates the 
                   //partialView, if it does change I can then pass ProjectId 
                   //to filter.
         },
         success: function(v) {
             RefreshData(v);
         },
         error: function(v, x, w) {
             //Error
         }
     });
 }
function RefreshData(v) {
     $("div#divMyIssues").html(v);
     return;         
 }
</script>

IssueController.cs

public ActionResult Index(int? page)
{
   // Load the Project List
   var projectList = new SelectList(_db.Project.ToList(), "ProjectId", "Name");
   ViewData["ProjectList"] = projectList;
   const int pageSize = 10;
   var myIssues = issueRepository.MyIssues2();
   var paginatedIssues = new PaginatedList<IssueSummary>(myIssues, page ?? 0, pageSize);
   ViewData.Model = paginatedIssues;
   // Adding for returning partial view
   //if (Request.IsAjaxRequest())
   //    return PartialView("MyIssues", paginatedIssues);
   //else
   //    return View(paginatedIssues);
   return View(paginatedIssues);
}

MyIssues.ascx强>

<ul>
 <% foreach (var m in ViewData.Model)
 { %>
    <li> <a href="<%= Url.RouteUrl("Default",  
       new { id = m.Id, controller = "Issue", action = "Details" })%>">
       <%= m.Title %></a>
  </li>
    <% } %>
</ul>
    <div class="pagination">
    <% if (Model.IsPreviousPage) { %>        
        <%= Html.RouteLink("<<<", "MyIssues", 
            new { page=(Model.PageIndex-1) }) %>        
    <% } %>        
    <% if (Model.IsNextPage) { %>        
        <%= Html.RouteLink(">>>", "MyIssues", 
            new { page = (Model.PageIndex + 1) })%>        
    <% } %>
</div>

How do I update data using Ajax, jQuery when I change a dropdown on my Index.aspx page?
I have a page with a ProjectList dropdown which should show all issues related to that Project.

If I change the return value on my Controller action, see the commented code, it either removes the master page and just loads the PartialView Or does not load anything. I also tried to do something like: return PartialView("MyIssues", paginatedIssues);

With the code below I am able to change the page using Ajax but am not returning the correct data or html to refresh the PartialView.

So what am I doing wrong or how can I accomplish the data refresh?

Index.aspx

<label for="ProjectList">Project:</label>
<%= Html.DropDownList("ProjectList", "--All--") %>
<div id="divMyIssues"><% Html.RenderPartial("MyIssues", Model); %></div>
<script type="text/javascript">
$(document).ready(function() {
     $("#ProjectList").change(function() {
         CanIRefresh();
     });
  });
  function CanIRefresh() {
     $.ajax({
         type: "POST",
         url: "/Issue/" + "Index",
         dataType: "html",
         data: {
             page: 5// just changing the page to see if it updates the 
                   //partialView, if it does change I can then pass ProjectId 
                   //to filter.
         },
         success: function(v) {
             RefreshData(v);
         },
         error: function(v, x, w) {
             //Error
         }
     });
 }
function RefreshData(v) {
     $("div#divMyIssues").html(v);
     return;         
 }
</script>

IssueController.cs

public ActionResult Index(int? page)
{
   // Load the Project List
   var projectList = new SelectList(_db.Project.ToList(), "ProjectId", "Name");
   ViewData["ProjectList"] = projectList;
   const int pageSize = 10;
   var myIssues = issueRepository.MyIssues2();
   var paginatedIssues = new PaginatedList<IssueSummary>(myIssues, page ?? 0, pageSize);
   ViewData.Model = paginatedIssues;
   // Adding for returning partial view
   //if (Request.IsAjaxRequest())
   //    return PartialView("MyIssues", paginatedIssues);
   //else
   //    return View(paginatedIssues);
   return View(paginatedIssues);
}

MyIssues.ascx

<ul>
 <% foreach (var m in ViewData.Model)
 { %>
    <li> <a href="<%= Url.RouteUrl("Default",  
       new { id = m.Id, controller = "Issue", action = "Details" })%>">
       <%= m.Title %></a>
  </li>
    <% } %>
</ul>
    <div class="pagination">
    <% if (Model.IsPreviousPage) { %>        
        <%= Html.RouteLink("<<<", "MyIssues", 
            new { page=(Model.PageIndex-1) }) %>        
    <% } %>        
    <% if (Model.IsNextPage) { %>        
        <%= Html.RouteLink(">>>", "MyIssues", 
            new { page = (Model.PageIndex + 1) })%>        
    <% } %>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-08-06 02:00:14

您可以使用很棒的 jQuery 表单插件: http://malsup.com/jquery/form/并让 ddl 更改事件提交表单。

$(document).ready(function() { 
    var options = { 
        target:'#divMyIssues',
    }; 

    $('form').ajaxForm(options); 
    $("#ProjectList").change(function() {
        $('form').submit();
    });

}); 

您可以在 API 文档中阅读目标选项的工作原理,尽管它很简单。

然后阅读本教程(但您似乎已经了解 Request.IsAjaxRequest())
http://www.asp.net/learn/mvc/tutorial- 32-cs.aspx

You could use the awesome jQuery Form Plugin: http://malsup.com/jquery/form/ and have the ddl change event submit the form.

$(document).ready(function() { 
    var options = { 
        target:'#divMyIssues',
    }; 

    $('form').ajaxForm(options); 
    $("#ProjectList").change(function() {
        $('form').submit();
    });

}); 

You can read in the API docs how the target option works, it's as simple as it gets though.

Then read this tutorial (but you already seem to know about Request.IsAjaxRequest())
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx

殤城〤 2024-08-06 02:00:14

不确定这会有帮助,但我在上周左右一直在做类似的事情。

我没有做$.ajax,我使用了$.Post。 但在视图端重要的是在语句末尾包含 , "json") 。 当我返回可以迭代的 Json 数据时使用它。 在后面的代码中,我必须执行 return Json(mydata ,以便我实际上可以遍历返回的集合。

我还在其他地方执行了 return PartialView("commentList" ,在客户端,我

$.post("/jQueryTests/jQueryAddMessageComment", { commentText: commentText }, function(newComment) {
            $("#divComments" + id.toString()).html(newComment);
        });

不确定这些是否有帮助,但对我有帮助的是从不小但非常小的开始,然后从那里开始构建,

如果您想要更多,请告诉我,我会尽力提供。但我不是 Jquery 专家。

Not sure this will help but I've been doing similar in the last week or so.

I didn't do $.ajax I used $.Post. But the important thing at the view end was to include , "json") at the end of the statement. Used that when I returned Json data that I could itterate through. In the code behind I then had to do return Json(mydata so that I could actually itterate through the returned collection.

I also did a return PartialView("commentList" elsewhere and at the client end I

$.post("/jQueryTests/jQueryAddMessageComment", { commentText: commentText }, function(newComment) {
            $("#divComments" + id.toString()).html(newComment);
        });

I'm not sure if any of this helps but what helped me was to start off not small but really small and build up from there.

If you want more then let me know and I'll try to provide. But I'm no Jquery expert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文