Html.DropDownList 使用 Ajax 调用来填充部分视图
我的页面中有一个下拉列表:(结果是一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您摆脱 MSAjax 并使用 jquery 和 表单插件:
并在单独的js 文件:
I would recommend you to get rid of MSAjax and use jquery along with the form plugin:
And in a separate js file:
这是因为
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 theif (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