2015-11-20 129 views

回答

0

Asp.Net-5帶來了一個名爲「TagHelpers」的新功能。

你看到他們在默認MVC6工作共享_Layout.cshtml頁面產生不同的輸出取決於運行時環境的「環境」的標籤 - 開發,分期,生產等

你見參考文獻‘在Solution Explorer的部分「的內Microsoft.AspNet.MVC.TagHelpers ......」包中同時DNX 4.5.1和DNX核心5.0框架段裏裝’了。

看起來好像TagHelpers被設計成替換爲'模板'

這裏快速瀏覽:Wildermuth

而且兩篇文章更深入:MSDNMSDN Mag - 2部分的文章

希望有所幫助你。

+0

標籤助手沒有被設計作爲替換對模板,而是使用'@ Html'helpers和co。在HTML中。贊成真正的XML的HTML。 – ProfK

+0

ProfK - 不是指「項目模板」;但是,正如你指出的那樣,由@Html助手生成的模板。 –

2

基於DefaultEditorTemplates類我製作了TagHelper。

也許對某人來說它會有用。

ModelTagHelper.cs

[HtmlTargetElement("div", Attributes = ForAttributeName)] 
public class ModelTagHelper : TagHelper 
{ 
    protected const string ForAttributeName = "asp-for"; 
    protected const string ObjectViewPath = "~/Views/Shared/Object.cshtml"; 

    [HtmlAttributeNotBound] 
    [ViewContext] 
    public ViewContext ViewContext { get; set; } 

    [HtmlAttributeName(ForAttributeName)] 
    public ModelExpression For { get; set; } 

    public override int Order => -1000; 

    protected object Model => For.Model; 

    protected IHtmlGenerator HtmlGenerator { get; } 
    protected IViewEngine ViewEngine { get; } 
    protected ITempDataProvider TempDataProvider { get; } 
    protected IHttpContextAccessor HttpContextAccessor { get; } 

    public ModelTagHelper(
     ICompositeViewEngine viewEngine, 
     ITempDataProvider tempDataProvider, 
     IHttpContextAccessor httpContextAccessor, 
     IHtmlGenerator generator) 
    { 
     HtmlGenerator = generator; 
     ViewEngine = viewEngine; 
     TempDataProvider = tempDataProvider; 
     HttpContextAccessor = httpContextAccessor; 
    } 

    protected virtual ActionContext CreateActionContext() => new ActionContext(HttpContextAccessor.HttpContext, new RouteData(), new ActionDescriptor()); 
    protected virtual ViewDataDictionary CreateViewDataDictionary() => new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()); 

    public override void Process(TagHelperContext context, TagHelperOutput output) 
    { 
     if (context == null) 
     { 
      throw new ArgumentNullException(nameof(context)); 
     } 

     if (output == null) 
     { 
      throw new ArgumentNullException(nameof(output)); 
     } 

     using (var sw = new StringWriter()) 
     { 
      var viewDataDictionary = CreateViewDataDictionary(); 
      viewDataDictionary.Model = Model; 

      var actionContext = CreateActionContext(); 
      var viewResult = ViewEngine.FindPartialView(actionContext, ObjectViewPath); 
      if (!viewResult.Success) 
      { 
       throw new FileNotFoundException("Cannot find the file", ObjectViewPath); 
      } 
      var viewContext = new ViewContext(
       actionContext, 
       viewResult.View, 
       viewDataDictionary, 
       new TempDataDictionary(HttpContextAccessor, TempDataProvider), 
       sw, new HtmlHelperOptions { ClientValidationEnabled = true }); 
      viewResult.View.RenderAsync(viewContext).Wait(); 
      sw.Flush(); 

      output.Content.SetContent(new HtmlString(sw.ToString())); 
     } 
    } 

} 

Object.cshmtl

@using Microsoft.AspNet.Mvc.ViewFeatures 
@{ 
var tplInfo = ViewContext.ViewData.TemplateInfo; 

Func<ModelExplorer, TemplateInfo, bool> ShouldShow = 
    (modelExplorer, templateInfo) => 
    modelExplorer.Metadata.ShowForEdit && 
    !modelExplorer.Metadata.IsComplexType && 
    !templateInfo.Visited(modelExplorer); 

var properties = from property in ViewData.ModelExplorer.Properties 
       let propertyMetadata = property.Metadata 
       where ShouldShow(property, tplInfo) 
       select propertyMetadata; 

} 
@foreach (var propertyMetadata in properties) 
{ 
if (propertyMetadata.HideSurroundingHtml) 
{ 
    @Html.Editor(propertyMetadata.PropertyName) 
} 
else 
{ 
    var label = Html.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null); 
    if (!string.IsNullOrEmpty(label.ToString())) 
    { 
     <div class="editor-label"> 
      @label 
     </div> 
    } 
    <div class="editor-field"> 
     @Html.Editor(propertyMetadata.PropertyName) 
     @Html.ValidationMessage(propertyMetadata.PropertyName) 
    </div> 
} 
} 

MyView.cshtml

<div asp-for="@Model"></div>