我有一个简单的问题。
我有以下HTML代码
<a href="#" class="purchase">
<span>Purchase</span><em>@string.Format("{0:C0}", Model.thisItem.ItemSalePrice)</em>
</a>
但我想使用Ajax.Actionlink将其作为ajax帖子。
我有以下更新的代码
@Ajax.ActionLink("Purchase", "AddToCart", "Browse", new { ItemID = Model.thisItem.ItemID }, new AjaxOptions { UpdateTargetId = "content" }, new { @class = "purchase" })
问题是我如何将标签放入actionlink?
谢谢大家。
更新
我也从ASP.NET的论坛得到了答案,它更容易。 http://forums.asp.net/p/1702210/4518688.aspx/1?p=True&t=634468566867949718
答案 0 :(得分:0)
只需在操作链接文本参数中指定它:
@Ajax.ActionLink(String.Format("{0} {1}", "Purchase", String.Format("{0:C0}", Model.thisItem.ItemSalePrice)), "AddToCart", "Browse", new { ItemID = Model.thisItem.ItemID }, new AjaxOptions { UpdateTargetId = "content" }, new { @class = "purchase" })
编辑1: 其他重载未显示,但这应该足以让您入门。 如果您需要更多详细信息,可以查看MVC3源代码并了解他们如何设置帮助程序。
public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, float salePrice, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
return CustomActionLink(htmlHelper, linkText, salePrice, actionName, controllerName, new RouteValueDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, float salePrice, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
var url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
// Build the <span> tag
var spanBuilder = new TagBuilder("span");
spanBuilder.SetInnerText(linkText);
string spanHtml = spanBuilder.ToString(TagRenderMode.Normal);
// Build the <em> tag
var emBuilder = new TagBuilder("em");
emBuilder.SetInnerText(String.Format("{0:C0}", salePrice));
string emHtml = emBuilder.ToString(TagRenderMode.Normal);
// Build the <a> tag
var anchorBuilder = new TagBuilder("a");
anchorBuilder.MergeAttribute("href", url.Action(actionName, controllerName, routeValues));
anchorBuilder.InnerHtml = spanHtml + emHtml;
string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(anchorHtml);
}
注意:HtmlHelper.AnonymousObjectToHtmlAttributes
位于System.Web.Mvc
命名空间下。