【问题标题】:Call to two buttons from the same page is not working从同一页面调用两个按钮不起作用
【发布时间】:2014-06-26 12:40:30
【问题描述】:

我有一个带有表格的 MVC5 应用程序,当您单击创建时,它会导航到带有表单的新页面以放置数据并保存。目前这工作正常,问题是我需要添加一个额外的按钮来调用新操作但不会导航到其他页面,只需调用它并获得响应。

当我将以下 2 个按钮放在不同的页面中时,它们都可以正常工作,但是当我将它们添加到同一页面时,第二个按钮会调用第一个按钮的创建操作。

如何避免这种情况?

这是调用创建操作的保存按钮

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" id="actbtn" value="Create"  />
    </div>
</div>

这是第二个在不同页面中正常工作的按钮,当我复制它时 到它调用 create 操作 的创建页面。我知道这是因为 submit 但我应该如何避免呢?

    @using (Html.BeginForm("Check", "User", new { Name = Model.Name }))
    {
        <input type="submit" id="btn" value="Check" />
        <span id='result'></span>
    }

我应该如何调用控制器用户中的动作检查?

【问题讨论】:

    标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    像这样为每个按钮添加一个名称属性

    <input type="submit" value="Send" class="btn btn-default" name="buttonType" />
    <input type="submit" value="Search" class="btn btn-default" name="buttonType" />
    

    在控制器动作中,

    [HttpPost, ActionName("Index")]
    [ValidateAntiForgeryToken]
    public ActionResult Index(string buttonType)
    {
      if (buttonType == "Send")
         {
              // add your stuffs for the first button 
         }
      else
         {
              // add your stuffs for the second buttonbutton
         }
    }
    

    【讨论】:

      【解决方案2】:

      这是你需要做的..

      @using (Html.BeginForm("YourSaveActionName", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
      {
            <div class="form-group">
               <div class="col-md-offset-2 col-md-10">
                  <input type="submit" id="actbtn" value="Create"  />
               </div>
            </div>
      }
      
      
      @using (Html.BeginForm("Check", "User", new { Name = Model.Name }))
      {
           <input type="submit" id="btn" value="Check" />
           <span id='result'></span>
      }
      

      【讨论】:

      • 我试了一下,我遇到了同样的问题,当我点击第二个按钮时,我得到了错误 404,并且在 URL 中看到了路径操作/控制器,如何避免它,我想要留在同一页面并在行动中停止 BP...
      • 使用这个@using (Html.BeginForm("YourSaveActionName", "User", FormMethod.Post, new { enctype = "multipart/form-data" })),或者查看我编辑的答案
      • 谢谢,我添加了它,没有任何改变:(,还有其他想法吗?
      • 给我看你完整的 html 代码,你一定漏掉了什么。因为这在我的项目中适用于单页上的 5 个按钮。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2018-10-09
      • 1970-01-01
      • 2021-07-07
      • 2016-09-21
      • 2017-04-27
      • 2016-01-15
      相关资源
      最近更新 更多