将 DropDownList 的选定项目传递给 ajaxy 调用的操作

发布于 2024-12-27 15:34:02 字数 492 浏览 0 评论 0原文

我需要以 ajaxy 方式将列表中所选项目的值传递给操作:

View:

<%= Html.DropDownList("ApplicationColumns", applicationColumns, new { @style = "width: 200px;" })%>
<%= Ajax.ActionLink("AddColumnToTrim", "AddColumnToTrim", new { columnName = ??? }, new AjaxOptions() { UpdateTargetId = "columnsDiv" })%>
<div id="columnsDiv"></div>

我这样做是因为我无法发布整个表单,只需要根据所选项目和显示执行一些逻辑它在局部视图中。 如何将 DropDownList 所选项目的值(或所选 SelectListItem)传递给我的操作?谢谢。

I need to pass the value of on of the selected item in the list to the action in ajaxy way:

View:

<%= Html.DropDownList("ApplicationColumns", applicationColumns, new { @style = "width: 200px;" })%>
<%= Ajax.ActionLink("AddColumnToTrim", "AddColumnToTrim", new { columnName = ??? }, new AjaxOptions() { UpdateTargetId = "columnsDiv" })%>
<div id="columnsDiv"></div>

I'm doing this cause I cannot post the whole form and just need to execute some logic depending on the chosen item and display it in partial view.
How can I pass the value(or selected SelectListItem) of the selected item of the DropDownList to my action please ? Thank you.

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

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

发布评论

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

评论(1

枫以 2025-01-03 15:34:02

使用表单将是执行此操作的语义上正确的方法:

<% using (Ajax.BeginForm("AddColumnToTrim", "SomeController", new AjaxOptions { UpdateTargetId = "columnsDiv" })) { %>
    <%= Html.DropDownList("ApplicationColumns", applicationColumns, new { @style = "width: 200px;" })%>
    <button type="submit">AddColumnToTrim</button>
<% } %>

现在,提交此表单时,下拉列表的选定值将使用 AJAX 调用自动发送到控制器操作:

public ActionResult AddColumnToTrim(string ApplicationColumns)
{
    ...
}

并且,是的,您可以 设置此按钮的样式使其看起来像一个锚点。

Using a form would be the semantically correct way to do this:

<% using (Ajax.BeginForm("AddColumnToTrim", "SomeController", new AjaxOptions { UpdateTargetId = "columnsDiv" })) { %>
    <%= Html.DropDownList("ApplicationColumns", applicationColumns, new { @style = "width: 200px;" })%>
    <button type="submit">AddColumnToTrim</button>
<% } %>

Now the selected value of the dropdown will be automatically sent to the controller action using an AJAX call when this form is submitted:

public ActionResult AddColumnToTrim(string ApplicationColumns)
{
    ...
}

And, yes, you could style this button to look like an anchor.

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