Adding ActionLink
This commit is contained in:
parent
c8ede78582
commit
4e1511ed4d
|
|
@ -68,7 +68,7 @@
|
|||
<p>
|
||||
Go to our cool travel reservation system.
|
||||
</p>
|
||||
<p><a class="btn btn-default" href="@Url.Action("Fly", "Flight", new { area = "Travel" })">Reserve Now</a></p>
|
||||
<p>@Html.ActionLink("Reserve Now", "Fly", "Flight", new { area = "Travel" }, new { @class = "btn btn-default" })</p>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<h2>Get more libraries</h2>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq.Expressions;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
|
@ -88,6 +87,20 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
|
||||
protected IModelMetadataProvider MetadataProvider { get; private set; }
|
||||
|
||||
public HtmlString ActionLink(
|
||||
[NotNull] string linkText,
|
||||
string actionName,
|
||||
string controllerName,
|
||||
string protocol,
|
||||
string hostname,
|
||||
string fragment,
|
||||
object routeValues,
|
||||
object htmlAttributes)
|
||||
{
|
||||
var url = _urlHelper.Action(actionName, controllerName, routeValues);
|
||||
return GenerateLink(linkText, url, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a dictionary from an object, by adding each public instance property as a key with its associated
|
||||
/// value to the dictionary. It will expose public properties from derived types as well. This is typically used
|
||||
|
|
@ -483,6 +496,22 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
return theForm;
|
||||
}
|
||||
|
||||
protected virtual HtmlString GenerateLink(
|
||||
[NotNull] string linkText,
|
||||
[NotNull] string url,
|
||||
IDictionary<string, object> htmlAttributes)
|
||||
{
|
||||
var tagBuilder = new TagBuilder("a")
|
||||
{
|
||||
InnerHtml = WebUtility.HtmlEncode(linkText),
|
||||
};
|
||||
|
||||
tagBuilder.MergeAttributes(htmlAttributes);
|
||||
tagBuilder.MergeAttribute("href", url);
|
||||
|
||||
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
|
||||
}
|
||||
|
||||
protected virtual HtmlString GenerateTextBox(ModelMetadata metadata, string name, object value, string format,
|
||||
IDictionary<string, object> htmlAttributes)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.AspNet.Mvc.Core;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
|
||||
namespace Microsoft.AspNet.Mvc.Rendering
|
||||
{
|
||||
public static class HtmlHelperLinkExtensions
|
||||
{
|
||||
public static HtmlString ActionLink<TModel>(
|
||||
[NotNull] this IHtmlHelper<TModel> helper,
|
||||
[NotNull] string linkText,
|
||||
string actionName)
|
||||
{
|
||||
return helper.ActionLink(
|
||||
linkText,
|
||||
actionName,
|
||||
controllerName: null,
|
||||
protocol: null,
|
||||
hostname: null,
|
||||
fragment: null,
|
||||
routeValues: null,
|
||||
htmlAttributes: null);
|
||||
}
|
||||
|
||||
public static HtmlString ActionLink<TModel>(
|
||||
[NotNull] this IHtmlHelper<TModel> helper,
|
||||
[NotNull] string linkText,
|
||||
string actionName,
|
||||
object routeValues)
|
||||
{
|
||||
return helper.ActionLink(
|
||||
linkText,
|
||||
actionName,
|
||||
controllerName: null,
|
||||
protocol: null,
|
||||
hostname: null,
|
||||
fragment: null,
|
||||
routeValues: routeValues,
|
||||
htmlAttributes: null);
|
||||
}
|
||||
|
||||
public static HtmlString ActionLink<TModel>(
|
||||
[NotNull] this IHtmlHelper<TModel> helper,
|
||||
[NotNull] string linkText,
|
||||
string actionName,
|
||||
object routeValues,
|
||||
object htmlAttributes)
|
||||
{
|
||||
return helper.ActionLink(
|
||||
linkText,
|
||||
actionName,
|
||||
controllerName: null,
|
||||
protocol: null,
|
||||
hostname: null,
|
||||
fragment: null,
|
||||
routeValues: routeValues,
|
||||
htmlAttributes: htmlAttributes);
|
||||
}
|
||||
|
||||
public static HtmlString ActionLink<TModel>(
|
||||
[NotNull] this IHtmlHelper<TModel> helper,
|
||||
[NotNull] string linkText,
|
||||
string actionName,
|
||||
string controllerName)
|
||||
{
|
||||
return helper.ActionLink(
|
||||
linkText,
|
||||
actionName,
|
||||
controllerName,
|
||||
protocol: null,
|
||||
hostname: null,
|
||||
fragment: null,
|
||||
routeValues: null,
|
||||
htmlAttributes: null);
|
||||
}
|
||||
|
||||
public static HtmlString ActionLink<TModel>(
|
||||
[NotNull] this IHtmlHelper<TModel> helper,
|
||||
[NotNull] string linkText,
|
||||
string actionName,
|
||||
string controllerName,
|
||||
object routeValues,
|
||||
object htmlAttributes)
|
||||
{
|
||||
return helper.ActionLink(
|
||||
linkText,
|
||||
actionName,
|
||||
controllerName,
|
||||
protocol: null,
|
||||
hostname: null,
|
||||
fragment: null,
|
||||
routeValues: routeValues,
|
||||
htmlAttributes: htmlAttributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +31,38 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
/// </summary>
|
||||
ViewDataDictionary<TModel> ViewData { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns an anchor element (a element) that contains a url path to the specified action.
|
||||
/// </summary>
|
||||
/// <param name="linkText">The inner text of the anchor element.</param>
|
||||
/// <param name="actionName">The name of the action.</param>
|
||||
/// <param name="controllerName">The name of the controller.</param>
|
||||
/// <param name="protocol">The protocol for the URL, such as "http" or "https".</param>
|
||||
/// <param name="hostname">The host name for the URL.</param>
|
||||
/// <param name="fragment">The URL fragment name (the anchor name).</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. This object is typically created using object initializer syntax.
|
||||
/// Alternatively, an <see cref="IDictionary{string, object}"/> instance containing the route parameters.
|
||||
/// </param>
|
||||
/// <param name="htmlAttributes">
|
||||
/// An object that contains the HTML attributes to set for the element. Alternatively, an
|
||||
/// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// An anchor element (a element).
|
||||
/// </returns>
|
||||
HtmlString ActionLink(
|
||||
[NotNull] string linkText,
|
||||
string actionName,
|
||||
string controllerName,
|
||||
string protocol,
|
||||
string hostname,
|
||||
string fragment,
|
||||
object routeValues,
|
||||
object htmlAttributes);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes an opening <form> tag to the response. When the user submits the form,
|
||||
/// the request will be processed by an action method.
|
||||
|
|
|
|||
Loading…
Reference in New Issue