runat 服务器的正确语法是带有 MVC 6 .net 核心和 bootstrap 的按钮

correct syntax for runat server a button with MVC 6 .net core and bootstrap

我想知道如何使用 bootstrap 在 MVC 6 核心 Web 应用程序中创建一个简单的按钮操作。因此,例如,我可以执行 sql 存储过程,或者只是从服务器检索日期,或者根据请求通过代码从服务器检索其他内容,并将其显示在文本框中。特别是我喜欢知道没有花哨装饰的最小代码。

<asp:???  input type="button" runat="server" onclick="btn_Click" class="btn btn-default">

或者也许

<div>
<button type="button" class="btn btn-default" runat="server" onclick="btn_Click">
</div>

这可能是一个简单的问题,但我对如何在 MVC-6 中而不是在旧版本或 asp 页

中完成感到困惑

它不再称为 MVC 6。现在是 ASP.NET Core 1.0。 runat="server" 未在 ASP.NET Core 1.0 中使用,因为它不支持 Web 表单,而是依赖于 MVC 范例。出于同样的原因,也没有 onclick 属性。

所以你的按钮可能看起来像:

<button type="submit" class="btn btn-default">Click Here</button>

控制器上的操作方法可能如下所示:

    [HttpPost]
    public IActionResult Post() {
        /*do work here*/

        return View();
    }



完整示例

在评论中,您要求提供一个示例,说明如果表单上有多个按钮,如何判断单击了哪个按钮。这是一个例子:

/Views/example/index.cshtml

<html>
    <body>
        <form asp-controller="example" asp-action="Index">
            <label>Value:</label><input name="someValue" type="text" maxlength="10" />
            <button name="btnOne" type="submit" class="btn btn-default">Click One</button>
            <button name="btnTwo" type="submit" class="btn btn-default">Click Two</button>
        </form>
    </body>
</html>

/Controllers/example/ExampleController.cs

using Microsoft.AspNetCore.Mvc;

namespace App.Web.Controllers {

    public class ExampleController: Controller {

        public ExampleController() {

        }

        [HttpGet]
        [Route("/example/")]
        public IActionResult Index() {
            return View();
        }


        [HttpPost]
        [Route("/example/")]
        public IActionResult Index(string someValue) {
            string buttonClicked = "";


            if(HttpContext.Request.Form.ContainsKey("btnOne")) {
                buttonClicked = "btnOne";
            } else if(HttpContext.Request.Form.ContainsKey("btnTwo")) {
                buttonClicked = "btnTwo";
            }

            return View("Index");
        }

    }
}

您可以在 ASP.NET Core 中了解有关表单的更多信息:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms
与网络表单相比,它们非常灵活,但一开始的学习曲线有点陡峭。