【问题标题】:Adding additional parameter to form submit向表单提交添加附加参数
【发布时间】:2020-02-01 16:03:15
【问题描述】:

我的 Razor 视图中有一个表单声明

<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"

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

【问题讨论】:

    标签: asp.net-mvc forms razor


    【解决方案1】:

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

    <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();
    });
    

    【讨论】:

      【解决方案2】:

      我知道你说过这不是问题,但你可以像这样向Html.BeginForm 添加属性:

       @using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post, new { id = "filterform" })){
      

      【讨论】:

        【解决方案3】:

        如何放置一个隐藏的输入

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

        在你的按钮上

        <input type="button" onclick="submitForm()" value="Show all" />
        

        在你的脚本某处

        function submitForm(){
        $('#showAll').val('true');
        $('#filterForm').submit();
        
        }
        

        【讨论】:

          【解决方案4】:

          如果您在表单中,则可以将路由添加到按钮元素。您可以使用 formAction 和 formMethod 覆盖表单的本机操作。您还可以使用其他 formActions 和 formMethods 拥有多个按钮

          <form action="/somewhere" method="get">
              <input type="type" name="name" value=" " />
          
          
              <button formaction="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true" formmethod="post">Show All</button>
          </form>

          【讨论】:

            【解决方案5】:

            你好,我发现这个很好用

             @using (Html.BeginForm("ActionName","ControllerName",  new { id = "filterform" },FormMethod.Post)){
            

            我已经厌倦了在参数之前执行“FormMethod.Post”,但它不起作用并且从未传递值。只有通过反复试验,我才设法解决它,并发现它应该像我发布的代码一样。

            希望有帮助

            【讨论】:

            【解决方案6】:

            如果同一个表单有多个提交按钮,并且需要为每个按钮传递不同的参数,则可以使用 asp-route 属性来完成此任务。

             <input type="submit" value="Save"/>
            
             <input id="btnComplete" type="submit"
                    asp-route-addNewOneAfterSave="@true"
                    value="Save And Create New One">
            

            请记住在 OnPost 方法中包含您的参数,例如:

            public async Task<IActionResult> OnPostAsync(bool? addNewOneAfterSave)
                    {
                         //Your code ....
                    }
            

            您可以从article. 阅读更多关于 asp-route 锚标记助手的信息

            【讨论】:

              猜你喜欢
              • 2023-04-08
              • 2016-05-22
              • 2013-03-03
              • 1970-01-01
              • 1970-01-01
              • 2011-09-15
              • 1970-01-01
              • 2011-02-01
              • 2021-10-29
              相关资源
              最近更新 更多