【问题标题】:Calling MVC Controller Action using Html button使用 Html 按钮调用 MVC 控制器操作
【发布时间】:2015-04-16 14:56:37
【问题描述】:

我是 MVC 的新手。我有一个“创建”按钮。我想在我的控制器中创建另一个调用“部署”操作的按钮。这个按钮基本上是提交表单并被部署。目前,表单已提交,但代码未进入“部署”功能

这是我尝试过的:-

<input type="submit" class="btn btn-primary" value="Create" />
    <input type="submit" class="btn btn-primary" value="Create and deploy" onclick="location.href='@Url.Action("Deploy", "MyController")' "/>

我的 Deploy 函数需要一个类似这样的参数:-

<a href="@Url.Action("Deploy", new {Id = Model.Id })">Deploy</a>

编辑

这就是我的代码现在的样子:-

@using (Html.BeginForm("Create", "MyController", FormMethod.Post, new { data_bind = "submit: formSubmit" }))
{
  <input type="submit" name ="create" class="btn btn-primary" value="Create" />
  <input type="submit" name="create" class="btn btn-primary" value="Create and deploy"/>
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyModel entity, string submitType)
{
  if (submitType == Create and Deploy)
  {
    RedirectToAction("Deploy", "MyController");
  }
  //Code to create
}

public ActionResult Deploy(string Id)
{
}

我该如何解决这个问题?

【问题讨论】:

  • @StephenMuecke 感谢您的回复。你能详细说明你的答案吗?
  • @StephenMuecke 我做到了。仍然给我相同的输出。我已经编辑了我的问题,我还需要将参数传递给 Deploy 函数。我想它不起作用,因为我没有通过它。你能帮我解决这个问题吗
  • @StephenMuecke 很抱歉造成混淆,我已将我的编辑放在问题中。你现在可以检查吗?我得到了 submitType =null
  • @StephenMuecke 我想做提交和部署。使用 @Html.ActionLink("Deploy", "Guage", new { Id = Model.ID }) 不会直接部署吗?
  • @StephenMuecke 基本上我有两个按钮 1) 创建 2) 创建和部署。在创建表单时应该只提交而不是部署。随着创建和部署表单应该提交并部署。

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


【解决方案1】:

为每个按钮添加一个客户端功能。

函数会有ajax调用,你可以用你需要的ActionName和ControllerName来设置Url属性

【讨论】:

    【解决方案2】:

    如果你给它一个name 属性,提交按钮的值将会回传。将按钮的 html 更改为

    <input type="submit" class="btn btn-primary" name="submitType" value="Create" />
    <input type="submit" class="btn btn-primary" name="submitType" value="Create and deploy" />
    

    和控制器方法

    public ActionResult Create(MyModel entity, string submitType)
    {
      if (!ModelState.IsValid)
      {
        return View(entity);
      }
      // Save the model
      ....
      // Check which button submitted the form
      if(submitType == "Create")
      {
        // redirect somewhere ?
      }
      else
      {
        // assuming you want to pass the value of entity.Id to the Deploy() method
        RedirectToAction("Deploy", "MyController", new { Id = entity.Id });
      }
    }
    

    【讨论】:

    • 感谢您的回答
    【解决方案3】:
     @using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
                {
                <input type="submit" class="btn btn-primary" value="Create and deploy"/>
                }
    
    @using (Html.BeginForm("Create", "MyController", FormMethod.Post))
                {
                <input type="submit" name ="create" class="btn btn-primary" value="Create" />
                }
    

    【讨论】:

      【解决方案4】:
       @using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
                      {
                      <input type="submit" class="btn btn-primary" value="Create and deploy"/>
                      }
      
      
       @using (Html.BeginForm("Create", "MyController", FormMethod.Post))
                      {
                      <input type="submit" name ="create" class="btn btn-primary" value="Create" />
                      }
      

      或者你需要改变点击按钮时表单的动作

      function deploybtnClick{
      document.getElementById('formId').action = 'Deploy';
      }
      

      【讨论】:

        【解决方案5】:

        使用这种方式点击该部署操作

         @using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
                        {
                        <input type="submit" class="btn btn-primary" value="Create and deploy"/>
                        }
        

        【讨论】:

        • 我已经将它指定为:- ` @using (Html.BeginForm("Create", "Mycontroller", FormMethod.Post, new { data_bind = "submit: formSubmit" }))` 应该我改了吗?
        • 这个答案假设你想去Deploy方法无论你点击哪个按钮(我不认为你想要)
        • 你必须添加单独的“@using (Html.BeginForm ......”来创建和部署,否则它只会在创建时命中。
        • 第一种方式不需要使用jquery
        猜你喜欢
        • 2011-01-31
        • 2012-09-23
        • 1970-01-01
        • 2018-08-24
        • 2018-11-26
        • 1970-01-01
        • 2011-02-12
        • 2013-01-11
        • 2013-02-16
        相关资源
        最近更新 更多