Html.DropDownList 使用 Ajax 调用来填充部分视图

发布于 2024-09-30 07:48:00 字数 820 浏览 1 评论 0原文

我的页面中有一个下拉列表:(结果是一个div)

  <% 
    using (Ajax.BeginForm("MembersByClubSearch", new AjaxOptions { UpdateTargetId = "results" }))
    { 
      %>
      <%= Html.DropDownList("ddlClubs", new SelectList(Model.ClubNameList, "ClubID", "ClubName"), new { onchange = "this.form.submit();" })%>
      <%
    }
  %>

我的操作是

public ActionResult MembersByClubSearch(string query)
    {
members = ... // code to get the List<> of members

      if (Request.IsAjaxRequest())
      {
        return View("MembersByClubSearchResultUserControl", members);
      }
      else
      {
        return View(members);
      }
    }

但是我的下拉列表的提交不是AjaxRequest。 当我使用提交按钮时,它工作正常,但我想在用户更改下拉列表时提交,而不是更改下拉列表并单击按钮。

有什么想法吗?

谢谢,

菲利普

I have a dropdownlist in my page: (results is a div)

  <% 
    using (Ajax.BeginForm("MembersByClubSearch", new AjaxOptions { UpdateTargetId = "results" }))
    { 
      %>
      <%= Html.DropDownList("ddlClubs", new SelectList(Model.ClubNameList, "ClubID", "ClubName"), new { onchange = "this.form.submit();" })%>
      <%
    }
  %>

My Action is

public ActionResult MembersByClubSearch(string query)
    {
members = ... // code to get the List<> of members

      if (Request.IsAjaxRequest())
      {
        return View("MembersByClubSearchResultUserControl", members);
      }
      else
      {
        return View(members);
      }
    }

But the submit of my dropdownlist isn't an AjaxRequest.
When I use a submit button, it works fine, but I want to submit when the user changes the dropdown instead of changing dropdown AND clicking a button.

Any ideas?

thanks,

Filip

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

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

发布评论

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

评论(2

穿透光 2024-10-07 07:48:00

我建议您摆脱 MSAjax 并使用 jquery 和 表单插件

<% using (Html.BeginForm("MembersByClubSearch", "Home")) { %>
    <%= Html.DropDownListFor(
        x => x.SelectedClubId, 
        new SelectList(Model.ClubNameList, "ClubID", "ClubName"), 
        new { id = "club" }
    ) %>
<% } %>

并在单独的js 文件:

$(function() {
    // Ajaxify the form => this is for normal submit
    $('form').ajaxForm(function(result) {
        $('#results').html(result);
    });

    // When the value of the dropdown changes force an ajax submit
    $('#club').change(function() {
        $('form').ajaxSubmit();
    });
});

I would recommend you to get rid of MSAjax and use jquery along with the form plugin:

<% using (Html.BeginForm("MembersByClubSearch", "Home")) { %>
    <%= Html.DropDownListFor(
        x => x.SelectedClubId, 
        new SelectList(Model.ClubNameList, "ClubID", "ClubName"), 
        new { id = "club" }
    ) %>
<% } %>

And in a separate js file:

$(function() {
    // Ajaxify the form => this is for normal submit
    $('form').ajaxForm(function(result) {
        $('#results').html(result);
    });

    // When the value of the dropdown changes force an ajax submit
    $('#club').change(function() {
        $('form').ajaxSubmit();
    });
});
饮惑 2024-10-07 07:48:00

这是因为 this.form.submit(); javascript 函数在您的表单上执行传统的 HTTP POST。您需要将 AJAX 样式的 POST 发送到控制器,才能触发 if (Request.IsAjaxRequest()) 语句。

我将使用 JQuery 和 $.post() 函数,它允许您指定回调函数来更新部分视图。

这里有一个很好的例子可以帮助您: 带有 MVC 和 jQuery 的动态选择列表

This is because the this.form.submit(); javascript function performs a traditional HTTP POST on your form. You need to send an AJAX style POST to your controller instead in order to trigger the if (Request.IsAjaxRequest()) statement.

I would use JQuery and the $.post() function, which allows you to specify a callback function to update your partial view.

There is a good example here that may help you: Dynamic-Select-Lists-with-MVC-and-jQuery

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