【问题标题】:How to manage multiple submit button in my case in MVC 5?如何在 MVC 5 中管理多个提交按钮?
【发布时间】:2019-02-19 17:51:57
【问题描述】:

我是 MVC 的初学者,我正在使用 MVC 5

_Layout.cshtml 代码

_Layout.cshtml有导航栏,如果用户已经登录,它包含注销,更多按钮,另外,它会渲染身体部分(Index.cshtml

//Navbar
@if (ViewBag.name != null)
{
    <input type="submit" class="dropdown-item" formaction="Logout" value="Logout" id="btnLogout" />
}
else{
    <a class="nav-link" href="#">Login & Signup</a>
}
<input type="submit" class="dropdown-item" formaction="More" value="More" id="btnMore" />
//body
<div class="container body-content">
    @RenderBody()    //Index.cshtml
</div>

Index.cshtml

Index.cshtml它有一些链接,还有一些按钮和锚链接,它会调用部分视图(_Login

@using TestProject.Models
@model ViewSignInAndSignUp 

//some html code
<input type="submit" class="dropdown-item" formaction="Action1" value="Action1" id="btnAction1" />
<input type="submit" class="dropdown-item" formaction="Action2" value="Action1" id="btnAction2" />

@Html.Partial("_Login")   //partialview

_Login.cshtml

_Login.cshtml这个局部视图,如果用户访问任何链接,这个局部视图会弹出请求用户登录。这个局部视图按钮正在工作

@model TestProject.Models.SignIn
@using (Html.BeginForm("ControllerSignIn", "Home"))
{
    @Html.TextBoxFor(si => si.userName, new { @class = "form-control", @id = "txtLogin" })
    @Html.TextBoxFor(si => si.password, new { @class = "form-control", @id = "txtPassword", @type = "password" })
    <input type="submit" class="btn btn-sm btn-primary btn-rounded" value="Login" id="btnLoginSubmit" />
}

HomeController

[HttpPost]
public ActionResult Logout(string submitButton)
{
    // some coding
    return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult More(string str1)
{
    // some coding
    return RedirectToAction("Index", "Home");
}

[HttpPost]
public ActionResult Action1(string str1)
{
    // some coding
    return RedirectToAction("Index", "Home");
}

[HttpPost]
public ActionResult Action2(string str1)
{
    // some coding
    return RedirectToAction("Index", "Home");
}

现在,我如何管理来自_Layout.cshtml, Index.cshtml, _Login.cshtml(Partial view) 的所有按钮

注意

我的登录按钮可以使用,但是注销、更多、action1、action2 按钮无法使用

【问题讨论】:

  • 一个 submit 按钮提交 a form。因此,不清楚您的问题是否真的只是将元素设置为“看起来”像按钮,或者每个按钮的多个表单。
  • @Edsf 我只需要为每个按钮传递一些参数,比如登录按钮(登录按钮将传递参数用户名和密码)。以同样的方式我必须执行其他按钮
  • 如果你想调用单独的控制器动作,你必须将每个输入 type='submit' 包装成自己的形式。
  • 如上述评论所述,或者,您可以在 AJAX 帖子中创建所需的任何有效负载。
  • @Liamneesan 如果你不想使用 AJAX,你所有的部分都必须有一个表单来对应它的提交按钮。正如之前所说,提交按钮提交表单,除非您在 javascript 上编写自己的帖子,否则无法绕过它

标签: c# asp.net-mvc model-view-controller asp.net-mvc-5


【解决方案1】:

替换这个

<input type="submit" class="dropdown-item" formaction="Logout" value="Logout" id="btnLogout" />

有了这个

<a href="Account/logout">Logout</a>

如果与上述不同,请将 href 替换为您的应用程序的注销端点。删除任何指向注销端点的表单,因为它不再需要了。

【讨论】:

    【解决方案2】:

    提交按钮使用&lt;form&gt;标签中定义的动作URL提交表单,如果你想使用多个提交按钮,那么你需要将它们包装在具有不同action属性值的多个表单中。

    但是,这些操作方法不需要使用多个提交按钮,因为多余的&lt;form&gt; 标记效率低下。你可以使用@Html.ActionLink() helper 来调用它们(然后使用样式使这些链接看起来像一个按钮):

    @Html.ActionLink("Logout", "Logout", "Home", null, new { @class = "dropdown-item", @id = "btnLogout" })
    

    如果动作包含参数,您应该添加routeValues,如下例所示:

    @Html.ActionLink("Action1", "Action1", "Home", new { str1 = "SomeValue" }, new { @class = "dropdown-item", @id = "btnAction1" })
    

    并将 HTTP 方法更改为 GET,因为 ActionLink 助手生成带有 href 属性的锚点 &lt;a&gt; 标记,应该使用 GET 方法:

    [HttpGet]
    public ActionResult Logout()
    {
        // some coding
        return RedirectToAction("Index", "Home");
    }
    

    同样的处理方式也应该应用于MoreAction1Action2 操作方法。

    进一步参考:

    ActionLink helper

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-06
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      相关资源
      最近更新 更多