Asp.net mvc Mvc Html.ActionButton

Asp.net mvc Mvc Html.ActionButton,asp.net-mvc,Asp.net Mvc,有人写过吗?我希望它的行为像一个链接,但看起来像一个按钮。只有一个按钮的表单是不行的,我不想要任何帖子 最简单的方法是使用一个带有method=“get”的小表单标记,在其中放置一个提交按钮: <form method="get" action="/myController/myAction/"> <input type="submit" value="button text goes here" /> </form> 另外,与cdmckay的代码不

有人写过吗?我希望它的行为像一个链接,但看起来像一个按钮。只有一个按钮的表单是不行的,我不想要任何帖子

最简单的方法是使用一个带有
method=“get”
的小
表单
标记,在其中放置一个提交按钮:

<form method="get" action="/myController/myAction/">
    <input type="submit" value="button text goes here" />
</form>

另外,与cdmckay的代码不同,这段代码实际上可以编译;)我知道这段代码可能会有很多开销,但我希望您不需要在每个页面上多次运行它。如果您这样做了,您可能可以进行一系列优化。

Tomas回答的代码:

public static class HtmlExtensions
{
  public static string ActionButton(this HtmlHelper helper, string value, 
      string action, string controller, object routeValues)
  {
    UrlHelper urlHelper = new UrlHelper(helper.ViewContext);
    var action = urlHelper.Action(action, controller, routeValues);

    var html = new StringBuilder();
    html.AppendFormat("<form method='get' action'{0}'>", action).AppendLine()
        .AppendFormat("    <input type='submit' value='{0}' />", value).AppendLine()
        .AppendFormat("</form>").AppendLine();

    return html.ToString();
  }
}
public静态类
{
公共静态字符串操作按钮(此HtmlHelper帮助程序,字符串值,
字符串操作、字符串控制器、对象路由值)
{
UrlHelper UrlHelper=新的UrlHelper(helper.ViewContext);
var action=urlHelper.action(操作、控制器、路由值);
var html=新的StringBuilder();
AppendFormat(“,action).AppendLine()
.AppendFormat(“,value).AppendLine()
.AppendFormat(“”.AppendLine();
返回html.ToString();
}
}

如果您希望它的行为像链接,但“看起来”像按钮,只需将ActionLink与CSS类一起使用即可

<%: Html.ActionLink("Back", "Index", null, new { @class = "link-button" })%>

我修改了Tomas Lyckens代码以返回一个MvcHtmlString,而不仅仅是一个字符串。这确保输出是HTML,而不是作为文本转义。我还用xml很好地记录了它。多亏了托马斯,他做了所有真正的工作

    /// <summary>
    /// Returns an HTML submit button (enclosed in its own form) that contains the virtual path of the specified action.
    /// </summary>
    /// <param name="helper">The HTML helper instance that this method extends.</param>
    /// <param name="buttonText">The visible text of the button.</param>
    /// <param name="action">The name of the action.</param>
    /// <param name="controller">The name of the controller.</param>
    /// <param name="routeValues">An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.</param>
    /// <returns></returns>
    public static MvcHtmlString ActionButton(this HtmlHelper helper, string buttonText, string action, string controller, object routeValues)
    {
        string a = (new UrlHelper(helper.ViewContext.RequestContext)).Action(action, controller, routeValues);

        var form = new TagBuilder("form");
        form.Attributes.Add("method", "get");
        form.Attributes.Add("action", a);

        var input = new TagBuilder("input");
        input.Attributes.Add("type", "submit");
        input.Attributes.Add("value", buttonText);

        form.InnerHtml = input.ToString(TagRenderMode.SelfClosing);

        return MvcHtmlString.Create(form.ToString(TagRenderMode.Normal));
    } 
//
///返回包含指定操作的虚拟路径的HTML提交按钮(以自己的形式封装)。
/// 
///此方法扩展的HTML帮助程序实例。
///按钮的可见文本。
///操作的名称。
///控制器的名称。
///包含管线参数的对象。通过检查对象的属性,通过反射检索参数。该对象通常是使用对象初始值设定项语法创建的。
/// 
公共静态MVCHTMLSING操作按钮(此HtmlHelper帮助程序、字符串按钮文本、字符串操作、字符串控制器、对象路由值)
{
字符串a=(新的UrlHelper(helper.ViewContext.RequestContext)).Action(Action,controller,routeValue);
var表格=新标记生成器(“表格”);
form.Attributes.Add(“方法”、“获取”);
添加(“动作”,a);
var输入=新标记生成器(“输入”);
输入。属性。添加(“类型”、“提交”);
input.Attributes.Add(“value”,buttonText);
form.InnerHtml=input.ToString(TagRenderMode.SelfClosing);
返回MvcHtmlString.Create(form.ToString(TagRenderMode.Normal));
} 

两个版本用于扩展

<button 
    onclick="javascript:window.location=('@Url.Action("Review", "Order", null)')"
    >Review Order</button>
审查顺序
不引人注目的版本:

<button data-action="@Url.Action("Review", "Order", null)">Review Order</button>

$(document).on('click', "[data-action]", 
    function(e) { window.location = $(this).attr('data-action'); }
);
审查顺序
$(文档)。在('单击',“[数据操作]”)上,
函数(e){window.location=$(this.attr('data-action');}
);

如果您的用户没有打开javascript,那么form标签就是一个不错的选择。尽管如此,如果您的链接已经在表单中,这会使情况变得困难。但是,您可以更改操作和方法以获取。

这是一个VB.NET版本,它有一个额外的功能,控制器和路由参数是可选的,因此如果控制器名称与页面的当前/默认控制器相同,则无需重复控制器名称即可使用

Public Module MvcExtensions

    ''' <summary> 
    ''' Returns an HTML submit button (enclosed in its own form) that contains the virtual path of the specified action. 
    ''' </summary> 
    ''' <param name="helper">The HTML helper instance that this method extends.</param> 
    ''' <param name="text">Text displayed in the button.</param> 
    ''' <param name="action">Action name.</param> 
    ''' <param name="controller">Optional controller name, using current when null.</param> 
    ''' <param name="routeValues">
    ''' An object that contains the parameters for a route. The parameters are retrieved
    ''' through reflection by examining the properties of the object.
    ''' The object is typically created by using object initializer syntax.
    ''' </param> 
    ''' <returns>
    ''' HTML output.
    ''' </returns> 
    <Extension()> _
    Public Function ActionButton(helper As HtmlHelper, text As String, action As String,
                                 Optional controller As String = Nothing, Optional routeValues As Object = Nothing) As MvcHtmlString

        ' Validate parameters
        If String.IsNullOrEmpty(text) Then Throw New ArgumentNullException("text")

        ' Get post back URL for action
        Dim actionUrl As String = New UrlHelper(helper.ViewContext.RequestContext).Action(action, controller, routeValues)

        ' Build form tag with action URL
        Dim form = New TagBuilder("form")
        form.Attributes.Add("method", "get")
        form.Attributes.Add("action", actionUrl)

        ' Add button
        Dim input = New TagBuilder("input")
        input.Attributes.Add("type", "submit")
        input.Attributes.Add("value", text)
        form.InnerHtml = input.ToString(TagRenderMode.SelfClosing)

        ' Return result as HTML
        Return MvcHtmlString.Create(form.ToString(TagRenderMode.Normal))

    End Function

End Module
公共模块MvcExtensions
'''  
''返回包含指定操作的虚拟路径的HTML提交按钮(以自己的形式封装)。
'''  
''此方法扩展的HTML帮助程序实例。
按钮中显示的“”文本。
''动作名称。
''可选控制器名称,为空时使用current。
''' 
''包含路由参数的对象。将检索这些参数
''通过检查对象的属性进行反射。
''该对象通常使用对象初始值设定项语法创建。
'''  
''' 
''HTML输出。
'''  
_
公共函数ActionButton(助手为HtmlHelper,文本为字符串,操作为字符串,
可选控制器为String=Nothing,可选RouteValue为Object=Nothing)为MvcHtmlString
'验证参数
如果String.IsNullOrEmpty(文本),则抛出新的ArgumentNullException(“文本”)
'获取用于操作的回发URL
Dim actionUrl As String=New UrlHelper(helper.ViewContext.RequestContext).Action(操作、控制器、路由值)
'使用操作URL生成表单标记
Dim表单=新标记生成器(“表单”)
form.Attributes.Add(“方法”、“获取”)
form.Attributes.Add(“action”,actionUrl)
'添加按钮
Dim输入=新标记生成器(“输入”)
input.Attributes.Add(“键入”、“提交”)
input.Attributes.Add(“值”,文本)
form.InnerHtml=input.ToString(TagRenderMode.SelfClosing)
'以HTML格式返回结果
返回MvcHtmlString.Create(form.ToString(TagRenderMode.Normal))
端函数
端模块
然后可以像其他MVC控件一样调用它,页面上的代码最少

Public Module MvcExtensions

    ''' <summary> 
    ''' Returns an HTML submit button (enclosed in its own form) that contains the virtual path of the specified action. 
    ''' </summary> 
    ''' <param name="helper">The HTML helper instance that this method extends.</param> 
    ''' <param name="text">Text displayed in the button.</param> 
    ''' <param name="action">Action name.</param> 
    ''' <param name="controller">Optional controller name, using current when null.</param> 
    ''' <param name="routeValues">
    ''' An object that contains the parameters for a route. The parameters are retrieved
    ''' through reflection by examining the properties of the object.
    ''' The object is typically created by using object initializer syntax.
    ''' </param> 
    ''' <returns>
    ''' HTML output.
    ''' </returns> 
    <Extension()> _
    Public Function ActionButton(helper As HtmlHelper, text As String, action As String,
                                 Optional controller As String = Nothing, Optional routeValues As Object = Nothing) As MvcHtmlString

        ' Validate parameters
        If String.IsNullOrEmpty(text) Then Throw New ArgumentNullException("text")

        ' Get post back URL for action
        Dim actionUrl As String = New UrlHelper(helper.ViewContext.RequestContext).Action(action, controller, routeValues)

        ' Build form tag with action URL
        Dim form = New TagBuilder("form")
        form.Attributes.Add("method", "get")
        form.Attributes.Add("action", actionUrl)

        ' Add button
        Dim input = New TagBuilder("input")
        input.Attributes.Add("type", "submit")
        input.Attributes.Add("value", text)
        form.InnerHtml = input.ToString(TagRenderMode.SelfClosing)

        ' Return result as HTML
        Return MvcHtmlString.Create(form.ToString(TagRenderMode.Normal))

    End Function

End Module

这应该从一开始就进入核心MVC框架;这似乎是一个显而易见的要求。我认为当用户执行创建或更改内容的操作时,按钮比链接更直观。链接应该只导航到相关信息(不更改任何内容)。如果我有一个网格,我会使用ActionLink进行任何数据导航(例如,单击产品名称转到产品页面),但只有ActionButton用于真正的“操作”,如编辑和删除。

如果您想要一个功能类似链接的按钮,这应该可以:

<input type="button" value="Button Text"
  onclick="@("location.href='http://stackoverflow.com/q/901372/2460971'")" />

如果您希望按钮使用控制器操作,只需稍作更改:

<input type="button" value="Button Text"
  onclick="@("location.href='" + Url.Action("ActionName", "ControllerName") + "'")" />

除了没有using语句外,st是什么
<input type="button" value="Button Text"
  onclick="@("location.href='" + Url.Action("ActionName", "ControllerName") + "'")" />