diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs new file mode 100644 index 0000000000..b1701904dc --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs @@ -0,0 +1,99 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; + +namespace Microsoft.AspNet.Mvc.TagHelpers +{ + /// + /// implementation targeting <option> elements. + /// + /// + /// This works in conjunction with . It has + /// in order to read element's content but does not modify that content. The + /// only modification it makes is to add a selected attribute in some cases. + /// + [ContentBehavior(ContentBehavior.Modify)] + public class OptionTagHelper : TagHelper + { + // Protected to ensure subclasses are correctly activated. Internal for ease of use when testing. + [Activate] + protected internal IHtmlGenerator Generator { get; set; } + + // Protected to ensure subclasses are correctly activated. Internal for ease of use when testing. + [Activate] + protected internal ViewContext ViewContext { get; set; } + + /// + /// Specifies that this <option> is pre-selected. + /// + /// + /// Passed through to the generated HTML in all cases. + /// + public string Selected { get; set; } + + /// + /// Specifies a value for the <option> element. + /// + /// + /// Passed through to the generated HTML in all cases. + /// + public string Value { get; set; } + + /// + /// + /// Does nothing unless contains a + /// entry and that entry is a non-empty + /// instance. Also does nothing if the associated <option> is already + /// selected. + /// + public override void Process(TagHelperContext context, TagHelperOutput output) + { + // Pass through attributes that are also well-known HTML attributes. + if (Value != null) + { + output.CopyHtmlAttribute(nameof(Value), context); + } + + if (Selected != null) + { + // This