asp.net-mvc - 向表单提交添加附加参数

标签 asp.net-mvc forms razor

我的 Razor View 中有一个表单声明

<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">

(顺便说一句,我选择这样写而不是使用 Html.BeginFrom 因为我需要给它一个 id 并且不知道如何用 Html.BeginFrom 来做到这一点 - 但这不是这里的问题)

在此表单之外,我有一个提交此表单的按钮(表单中还有一个提交按钮)
<input type="button" onclick="$('#filterForm').submit();" value="Show all" />

现在,问题是,如果使用此按钮提交表单,我希望表单中的操作更改为
action="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true"

如何更改操作并传递此附加参数?有没有更好的方法来做这一切?

最佳答案

将查询字符串参数附加到表单操作上,并尝试在运行时更改它是很棘手的(但并非不可能)。使用隐藏字段要容易得多:

<form method="post" action="/Lot/LotList" id="filterForm">
  <input type="hidden" name="auctionEventId" value="@auctionEventId" />
  ...

所以现在您要做的就是为“showAll”添加另一个隐藏字段
<form method="post" action="/Lot/LotList" id="filterForm">
  <input type="hidden" name="auctionEventId" value="@auctionEventId" />
  <input type="hidden" name="showAll" value="false" id="showAllField" />
  ...

只需在 showAll 按钮上连接一个 jquery 事件:
<input id="showAllButton" type="button"/>

jQuery:
$('#showAllButton').click(function(){
     $('#showAllField').val("true");
     $('#filterForm').submit();
});

关于asp.net-mvc - 向表单提交添加附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162819/

相关文章:

jquery - 如何在一页上处理多个表单?

Django 注册和登录 - 举例说明

java - 寻找在 JSP 中传递关联数组的替代方法

c# - 将复选框列表传递到 View 中并拉出 IEnumerable

javascript - 如何从 MVC 中的 javascript 调用服务器端函数?

c# - 没有 Entity Framework 的 ASP.NET 标识

c# - 未调用 kendogrid 更新

php - 在 Symfony 3 中为集合的每个元素应用特定的验证组

c# - MVC Razor : Helper result in html. 操作链接

asp.net - Visual Studio 始终打开网站而不是编辑器的网页