diff --git a/samples/MvcSample.Web/Views/Shared/MyView.cshtml b/samples/MvcSample.Web/Views/Shared/MyView.cshtml index 0fa8b00032..8b0000fb83 100644 --- a/samples/MvcSample.Web/Views/Shared/MyView.cshtml +++ b/samples/MvcSample.Web/Views/Shared/MyView.cshtml @@ -4,6 +4,11 @@ Layout = "/Views/Shared/_Layout.cshtml"; ViewBag.Title = "Home Page"; string nullValue = null; + + ViewBag.Anon = new + { + Address = new { Street = "123 Fake St.", City = "Redmond", State = "WA", Country = "USA", }, + }; } @functions { @@ -53,6 +58,46 @@

You can easily find a web hosting company that offers the right mix of features and price for your applications.

Learn more »

+ + +
+
+ + + + + + + + + + + + + + + + + +
+ + + '@Html.NameForModel()' +
+ + + '@Html.Name("Name")' +
+ + + '@Html.NameFor(m => m.Address)' +
+ + + '@Html.Name("Anon.Address.Street")' +
+
+
@await Component.InvokeAsync("Tags", 15)
diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index 12d009e356..4cb44bf881 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -130,6 +130,15 @@ namespace Microsoft.AspNet.Mvc.Rendering return TagBuilder.CreateSanitizedId(name, IdAttributeDotReplacement); } + /// + [SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", + MessageId = "1#", Justification = "This is a shipped API.")] + public virtual HtmlString Name(string name) + { + var fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name); + return new HtmlString(Encode(fullName)); + } + public async Task PartialAsync([NotNull] string partialViewName, object model, ViewDataDictionary viewData) { using (var writer = new StringWriter(CultureInfo.CurrentCulture)) diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs index 58f4c2d786..b2124fa2cc 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs @@ -1,5 +1,7 @@ using System; -using Microsoft.AspNet.Abstractions; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; +using Microsoft.AspNet.Mvc.Rendering.Expressions; namespace Microsoft.AspNet.Mvc.Rendering { @@ -36,5 +38,21 @@ namespace Microsoft.AspNet.Mvc.Rendering base.Contextualize(viewContext); } + + /// + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", + Justification = "This is an appropriate nesting of generic types")] + [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", + Justification = "Users cannot use anonymous methods with the LambdaExpression type")] + public HtmlString NameFor([NotNull] Expression> expression) + { + var expressionName = GetExpressionName(expression); + return Name(expressionName); + } + + protected string GetExpressionName([NotNull] Expression> expression) + { + return ExpressionHelper.GetExpressionText(expression); + } } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperNameExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperNameExtensions.cs new file mode 100644 index 0000000000..adf52f50ca --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperNameExtensions.cs @@ -0,0 +1,19 @@ + +namespace Microsoft.AspNet.Mvc.Rendering +{ + /// + /// Name-related extensions for and . + /// + public static class HtmlHelperNameExtensions + { + /// + /// Gets the full HTML field name for the current model. + /// + /// The instance that this method extends. + /// An that represents HTML markup. + public static HtmlString NameForModel([NotNull] this IHtmlHelper htmlHelper) + { + return htmlHelper.Name(string.Empty); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs index c28357e63c..00fac6ff97 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs @@ -1,4 +1,5 @@ using System; +using System.Linq.Expressions; using System.Threading.Tasks; namespace Microsoft.AspNet.Mvc.Rendering @@ -50,6 +51,21 @@ namespace Microsoft.AspNet.Mvc.Rendering /// The ID of the HTML element. string GenerateIdFromName(string name); + /// + /// Gets the full HTML field name for the given expression . + /// + /// Name of an expression, relative to the current model. + /// An that represents HTML markup. + HtmlString Name(string name); + + /// + /// Gets the full HTML field name for the given . + /// + /// The the returns. + /// An expression, relative to the current model. + /// An that represents HTML markup. + HtmlString NameFor([NotNull] Expression> expression); + /// /// Wraps HTML markup in an , which will enable HTML markup to be /// rendered to the output without getting HTML encoded. @@ -66,7 +82,6 @@ namespace Microsoft.AspNet.Mvc.Rendering /// An that represents HTML markup. HtmlString Raw(object value); - /// /// Returns a partial view in string format. ///