我有一个 html 助手:actionLink 在“a href”中生成一个“img”
所以这:
Html.ActionImage"Create", "Premises", "~/Images/Views/Premises/LicensableActivitiesButton.png", "Licensable Activities", new { id = "licensableActivitiesImg" })</div>
生成这个:
<a id="licensableActivitiesImg" href="/Users/Premises/Create"><img src="/Images/Views/Premises/LicensableActivitiesButton.png" alt="Licensable Activities">
问题是它将'/Users/'附加到href的开头,我不知道为什么。这个附加也以我的开始形式发生:
using (@Html.BeginForm("Create", "Premises", FormMethod.Post, new { id = "premisesForm" }))
给出:
<form id="premisesForm" method="post" action="/Users/Premises/Create">
我的路线很简单:
routes.MapRoute(
"",
"Premises/Premises/Create",
new { controller = "Premises", action = "Create" }
);
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
未使用的区域。
HTML 助手代码:
public static MvcHtmlString ActionImage(this HtmlHelper html, string action, string
Controller, string imagePath, string altText, object htmlAttributes, object routeValues)
{
var url = new UrlHelper(html.ViewContext.RequestContext);
// build the <img> tag
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute("src", url.Content(imagePath));
imgBuilder.MergeAttribute("alt", altText);
string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
// build the <a> tag
var anchorBuilder = new TagBuilder("a");
anchorBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues));
anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(anchorHtml);
}
任何想法/建议表示赞赏。