我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
当前回答
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
这应该对你有用。
其他回答
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
这应该对你有用。
HTML <button>元素只能回发到包含它的表单。
因此,您需要创建一个post到动作的表单,然后在表单中放入一个<button>或<input type="submit" />。
在所有的建议中,没有人使用razor语法(这也是bootstrap样式)。这将创建一个按钮,重定向到Account控制器中的Login视图:
<form>
<button class="btn btn-primary" asp-action="Login" asp-
controller="Account">@Localizer["Login"]</button>
</form>
你总是可以使用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")
最好用这个例子
<a href=“@Url.Action(”Register“,”Account“, new {id=Item.id })” class=“btn btn-primary btn-lg”>Register</a>