HTML按钮调用MVC控制器和Action方法

时间:2010-03-23 21:56:58

标签: html asp.net-mvc

我知道这是不对的,但为了说明,我想做这样的事情:

<%= Html.Button("Action", "Controller") %>

我的目标是制作一个HTML按钮,它将调用我的MVC控制器的动作方法。

19 个答案:

答案 0 :(得分:240)

除非您想要发布操作,否则根本不需要使用表单。输入按钮(不提交)将起到作用。

  <input type="button"
         value="Go Somewhere Else"
         onclick="location.href='<%: Url.Action("Action", "Controller") %>'" />

答案 1 :(得分:213)

Razor语法在这里:

<input type="button" value="Create" onclick="location.href='@Url.Action("Create", "User")'" />

答案 2 :(得分:61)

<button type="button" onclick="location.href='@Url.Action("MyAction", "MyController")'" />

type =&#34;按钮&#34; 阻止页面提交。相反,它会执行你的行动。

答案 3 :(得分:15)

您可以使用Url.Action指定生成控制器操作的网址,因此您可以使用以下任一方法:

<form method="post" action="<%: Url.Action("About", "Home") %>">
   <input type="submit" value="Click me to go to /Home/About" />
</form>

或:

<form action="#">
  <input type="submit" onclick="parent.location='<%: Url.Action("About", "Home") %>';return false;" value="Click me to go to /Home/About" />
  <input type="submit" onclick="parent.location='<%: Url.Action("Register", "Account") %>';return false;" value="Click me to go to /Account/Register" />
</form>

答案 4 :(得分:14)

试试这个:

@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)

这应该适合你。

答案 5 :(得分:8)

基于上述几个答案,您可以这样做:

<button onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />

答案 6 :(得分:8)

这是您可以将表单提交到Razor中的特定控制器和操作方法的方法。

 <input type="submit" value="Upload" onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />

答案 7 :(得分:6)

HTML <button>元素只能回发到包含它的表单。

因此,您需要制作一个POST表单,然后在表单中添加<button><input type="submit" />

答案 8 :(得分:5)

如果您收到“未终止的字符串常量”错误,请使用以下razor语法:

<input type="button" onclick="@("location.href='"+ Url.Action("Index","Test")+ "'")" />

答案 9 :(得分:4)

在控制器中实施操作时,请使用

return View("Index");

return RedirectToAction("Index");

其中已定义了Index.cshtml(或生成操作的页面)页面。否则你可能会遇到&#34;未找到视图或其主人......&#34;错误。

来源:https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/

答案 10 :(得分:4)

尽管有onclick方法,你也可以使用格式化如下:

<button type="submit" id="button1" name="button1" formaction='@Url.Action("Action", "Controller")'>Save</button>

答案 11 :(得分:3)

所以,我正在使用Razor,但这可以使用任何一种。我基本上是在链接中包含一个按钮。

<a href="Controller/ActionMethod">
    <input type="button" value="Click Me" />
</a>

答案 12 :(得分:3)

它更好地使用这个例子

&#13;
&#13;
<a href="@Url.Action("Register","Account", new {id=Item.id })"
class="btn btn-primary btn-lg">Register</a>
&#13;
&#13;
&#13;

答案 13 :(得分:1)

好的,你基本上需要将操作传递给按钮并在点击发生时调用它,它不需要在一个内部,你可以使用HTML {{1}点击按钮时触发按钮 ...

+CMTI: SM,<old used space>

答案 14 :(得分:1)

如果您在主页(&#34; / Home / Index&#34;)并且您想调用管理员控制器的索引操作,则以下内容适用于您。

<li><a href="/Admin/Index">Admin</a></li>

答案 15 :(得分:1)

在所有建议中,nobdy使用了razor语法(这也具有引导程序样式)。这将使一个按钮重定向到“帐户”控制器中的“登录”视图:

    <form>
        <button class="btn btn-primary" asp-action="Login" asp- 
   controller="Account">@Localizer["Login"]</button>
    </form>

答案 16 :(得分:0)

最好使用此示例

使用ActionLink调用操作和控制器:

@Html.ActionLink("Submit", "Action", "Controller", route, new { @class = "btn btn-block"})

答案 17 :(得分:0)

使用此示例:

<button name="nameButton" id="idButton" title="your title" class="btn btn-secondary" onclick="location.href='@Url.Action( "Index","Controller" new {  id = Item.id })';return false;">valueButton</button>

答案 18 :(得分:0)

您始终可以使用htmlHelpers并构建一些东西

    public static IHtmlContent BtnWithAction(this IHtmlHelper htmlHelper, string id, string text, string css="", string action="", string controller="")
    {
        try
        {
            string str = $"<button id=\"{id}\" class=\"{css}\" type=\"button\" ###>{text}</button>";
            if (!string.IsNullOrEmpty(action) && !string.IsNullOrEmpty(controller))
            {                    
                string url = ((TagBuilder)htmlHelper.ActionLink("dummy", action, controller)).Attributes["href"];
                var click = !string.IsNullOrEmpty(url) ? $"onclick=\"location.href='{url}'\"" : string.Empty;
                return new HtmlString(str.Replace("###", click));
            }
            return new HtmlString(str.Replace("###", string.Empty));
        }
        catch (Exception ex)
        {
            Log.Error(ex, ex.Message);
            var fkup = "<script>alert(\"assumption is the mother of all failures\")</script>";
            return new HtmlString(fkup);
        }
    }

然后在视图上这样称呼

@Html.BtnWithAction("btnCaretakerBack", "Back", "btn btn-primary float-right", "Index", "Caretakers")
相关问题