我有一个ActionLink:
<%: Html.ActionLink("MyAction", "MyAction") %>
我想用一个按钮代替。像这样:
<asp:Button ID="Button1" runat="server" Text="MyAction" />
点击按钮执行与单击ActionLink相同的操作需要做什么?
答案 0 :(得分:5)
actionlink与webforms中的Hyperlinkbutton或Hyperlink控件相当。它们都在html中呈现标准锚元素。所以是的,如果你想让它看起来像一个按钮,你应该在它上面添加一些css酱。
<%: Html.ActionLink("MyAction", "MyAction", null, new { @class = "button" } %>
并在css文件中:
.button {
outline: 0;
margin: 0 4px 0 0;
padding: 0 1em;
height: 2em;
text-decoration: none !important;
cursor: pointer;
position: relative;
text-align: center;
display: inline-block;
background-color:Green;
border:1px solid Lime;
}
a.button { color:White;}
注意:我是一名开发人员,因此设计方面存在挑战。使用border-left,-right,-bottom和-top可以让它看起来更漂亮。
第一条评论后更新:
对我来说看起来不太好的其他选择是:
<% Using (Html.BeginForm("About", "Home")) {%>
<input type="submit" value="About" />
<% } %>
或使用Button控件:
<form id="Form1" runat="server">
<asp:Button runat="server" ID="sfsf" Text ="Go!" PostBackUrl="/Home/About" />
</form>
答案 1 :(得分:0)
我有同样的问题,只是用html写了我想要的东西。
<input type="submit" value="Cancel" onclick="document.location = '/Product/Index'; return false;" />
我本来希望使用type =“button”,但由于使用了不同的样式,我将其设为提交按钮并添加了返回false。
答案 2 :(得分:0)
如果你想要一个真正的Ajax button
元素,而不是造型黑客或迷你形式或JQuery,它也可能只需要一些参与。遗憾的是MS尚未选择将ActionButton添加到Html和Ajax帮助程序,因为当您删除重复的私有支持方法时,差异实际上非常小(您只需要ActionButton
和{ {1}}方法如下所示。)
最终结果是你可以拥有像ajax动作链接那样触发的真实按钮:
GenerateButton
以下代码基于AjaxExtensions类的反编译,因为许多必需的帮助方法未在HtmlHelper上公开。
@Ajax.ActionButton("Delete", "Delete", "document",
new { id = ViewBag.Id },
new AjaxOptions()
{
Confirm="Do you really want to delete this file?",
HttpMethod = "Get",
UpdateTargetId = "documentlist" },
new { id = "RefreshDocuments"
})
jQuery jQuery junery.unobtrusive-ajax.js只需要进行一些小的更改就可以接受新的按钮对象,因为它非常接近开始。首先,选择器需要接受按钮和链接,然后href需要来自一个属性,因此非链接可以提供它(不是严格的浏览器兼容,但现在可以工作)。
public static partial class AjaxExtensions
{
public static MvcHtmlString ActionButton(this AjaxHelper ajaxHelper, string buttonText, string actionName, string controllerName, object routeValuesBlah, AjaxOptions ajaxOptions, object htmlAttributesBlah)
{
// Convert generic objects to specific collections
RouteValueDictionary routeValues = new RouteValueDictionary(routeValuesBlah);
RouteValueDictionary htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributesBlah);
if (string.IsNullOrEmpty(buttonText))
throw new ArgumentException("Button text must be provided");
string targetUrl = UrlHelper.GenerateUrl((string)null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);
return MvcHtmlString.Create(GenerateButton(ajaxHelper, buttonText, targetUrl, AjaxExtensions.GetAjaxOptions(ajaxOptions), htmlAttributes));
}
public static string GenerateButton(AjaxHelper ajaxHelper, string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
{
TagBuilder tagBuilder = new TagBuilder("input");
tagBuilder.MergeAttribute("value", linkText);
tagBuilder.MergeAttributes<string, object>(htmlAttributes);
tagBuilder.MergeAttribute("href", targetUrl);
tagBuilder.MergeAttribute("type", "button");
if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
tagBuilder.MergeAttributes<string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes());
else
tagBuilder.MergeAttribute("onclick", AjaxExtensions.GenerateAjaxScript(ajaxOptions, "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});"));
return tagBuilder.ToString(TagRenderMode.Normal);
}
private static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat)
{
string str = ajaxOptions.ToJavascriptString();
return string.Format((IFormatProvider)CultureInfo.InvariantCulture, scriptFormat, new object[1] { str });
}
private static AjaxOptions GetAjaxOptions(AjaxOptions ajaxOptions)
{
if (ajaxOptions == null)
return new AjaxOptions();
else
return ajaxOptions;
}
public static string ToJavascriptString(this AjaxOptions ajaxOptions)
{
StringBuilder stringBuilder = new StringBuilder("{");
stringBuilder.Append(string.Format((IFormatProvider)CultureInfo.InvariantCulture, " insertionMode: {0},", new object[1]
{
ajaxOptions.InsertionModeString()
}));
stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("confirm", ajaxOptions.Confirm));
stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("httpMethod", ajaxOptions.HttpMethod));
stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("loadingElementId", ajaxOptions.LoadingElementId));
stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("updateTargetId", ajaxOptions.UpdateTargetId));
stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("url", ajaxOptions.Url));
stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onBegin", ajaxOptions.OnBegin));
stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onComplete", ajaxOptions.OnComplete));
stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onFailure", ajaxOptions.OnFailure));
stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onSuccess", ajaxOptions.OnSuccess));
--stringBuilder.Length;
stringBuilder.Append(" }");
return ((object)stringBuilder).ToString();
}
public static string InsertionModeString(this AjaxOptions ajaxOptions)
{
switch (ajaxOptions.InsertionMode)
{
case InsertionMode.Replace:
return "Sys.Mvc.InsertionMode.replace";
case InsertionMode.InsertBefore:
return "Sys.Mvc.InsertionMode.insertBefore";
case InsertionMode.InsertAfter:
return "Sys.Mvc.InsertionMode.insertAfter";
default:
return ((int)ajaxOptions.InsertionMode).ToString((IFormatProvider)CultureInfo.InvariantCulture);
}
}
public static string EventStringIfSpecified(this AjaxOptions ajaxOptions, string propertyName, string handler)
{
if (string.IsNullOrEmpty(handler))
return string.Empty;
return string.Format((IFormatProvider)CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),",
new object[2]
{
propertyName,
handler
});
}
public static string PropertyStringIfSpecified(this AjaxOptions ajaxOptions, string propertyName, string propertyValue)
{
if (string.IsNullOrEmpty(propertyValue))
return string.Empty;
string str = propertyValue.Replace("'", "\\'");
return string.Format((IFormatProvider)CultureInfo.InvariantCulture, " {0}: '{1}',",
new object[2]
{
propertyName,
str
});
}
}
*注意:这是在回答日期使用最新版本的所有内容(MVC 5)