mvc html.button,asp.net mvc - Mvc Html.ActionButton - Stack Overflow

本文介绍了一个VB.NET实现的HTML辅助方法,用于创建MVC应用程序中的操作按钮。该方法简化了页面上提交按钮的创建过程,并允许指定动作、控制器及路由参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here is a VB.NET version with one extra thing, the controller and routes parameters are optional so it can be used without repeating the controller name if it's the same as the current/default controller for the page.

Public Module MvcExtensions

'''

''' Returns an HTML submit button (enclosed in its own form) that contains the virtual path of the specified action.

'''

''' The HTML helper instance that this method extends.

''' Text displayed in the button.

''' Action name.

''' Optional controller name, using current when null.

'''

''' 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.

'''

'''

''' HTML output.

'''

_

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

Then it can be invoked like the other other MVC controls, with minimum code on the page.

This should really have gone into the core MVC framework from the start; it seems such an obvious requirement. I think buttons are much more intuitive for the user when performing actions which create or change things, rather than links. Links should just navigate to related information (not change anything). If i had a grid I would use ActionLink for any data navigation (e.g. click a product name to goto product page) but only ActionButton for real "actions" like edit and delete.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值