, .
MVC 5 MVC Core HtmlHelpers, MVC Core [:-(], MVC Core, .
, , MVC Core, Razor.

ViewComponent, html, html.
HtmlHelper, .
HtmlHelpers html "ViewRole", , .
@model App.WebLib.AspNetCore.WebApp.Areas.AppHelpers.Models.AppHelpersViewModel
<div class="form-group">
@Html.LabelTagFor(m => m.TextInput, 4)
<div class="col-md-8">
@Html.TextTagFor(Model.ViewRole, m => m.TextInput)
</div>
</div>
@await Component.InvokeAsync("DisplaySource", new { executingViewPath = ViewContext.ExecutingFilePath, viewRole = Model.ViewRole })
@model App.WebLib.AspNetCore.WebApp.Areas.AppHelpers.Models.AppHelpersViewModel
@{
var displayModel = Model.GetDisplayCopy();
}
<form class="form-horizontal">
<div class="row">
<div class="col-md-9">
<h3>Estructura General de las Formas</h3>
@Html.Partial("_FormLayout")
<h3>Helper Básicos</h3>
<h4>Campo de texto</h4>
@Html.Partial("_TextInput")
@Html.Partial("_TextInput", displayModel)
<h4>Campo numérico</h4>
@Html.Partial("_NumberInput")
@Html.Partial("_NumberInput", displayModel)
</div>
</div>
</form>
Model
public class AppHelpersViewModel : ViewModelBase
{
public AppHelpersViewModel()
{
ViewRole = ViewRole.Edit;
}
[Display(Name = "Text Display Name", Prompt = "Text Display Prompt")]
public string TextInput { get; set; }
[Display(Name = "Number Display Name")]
public decimal NumberInput { get; set; }
[Display(Name = "Date Display Name")]
public DateTime? DateInput { get; set; }
[Display(Name = "Bool Display Name", Prompt ="Bool Display Prompt")]
public bool BoolInput { get; set; }
[Display(Name = "Required Text Input Label", Prompt = "Placeholder Text")]
[Required]
public string RequiredTextInput { get; set; }
public AppHelpersViewModel GetDisplayCopy()
{
var displayCopy = this.MemberwiseClone() as AppHelpersViewModel;
displayCopy.ViewRole = ViewRole.Display;
displayCopy.TextInput = "TextInput content";
displayCopy.RequiredTextInput = "RequiredTextInput content";
displayCopy.NumberInput = 45.4m;
displayCopy.DateInput = new DateTime(2016, 10, 24);
displayCopy.BoolInput = true;
return displayCopy;
}
}
ViewComponent
public class DisplaySourceViewComponent : ViewComponent
{
public DisplaySourceViewComponent(IHostingEnvironment hostingEnvironment)
{
WebRootPath = hostingEnvironment.WebRootPath;
ContentRootPath = hostingEnvironment.ContentRootPath;
}
public string ContentRootPath { get; set; }
public string WebRootPath { get; set; }
public async Task<IViewComponentResult> InvokeAsync(string executingViewPath, ViewRole viewRole)
{
if (viewRole != ViewRole.Display)
{
return new NullViewComponentResult();
}
IEnumerable<string> viewSource = await GetViewSourceAsync(executingViewPath);
return View(viewSource);
}
private int GetLastContentIndex(List<string> lines)
{
for (int i = lines.Count - 1; i >= 0; i--)
{
if (!String.IsNullOrWhiteSpace(lines[i]))
{
return i;
}
}
return -1;
}
private async Task<IEnumerable<string>> GetViewSourceAsync(string executingViewPath)
{
executingViewPath = executingViewPath.Substring(1).Replace('/', Path.DirectorySeparatorChar);
string viewFilePath = Path.Combine(ContentRootPath, executingViewPath);
var lines = new List<string>();
using (var reader = new StreamReader(viewFilePath, Encoding.UTF8))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (line.StartsWith("@model")) continue;
if (line.StartsWith("@await") && line.Contains(@"InvokeAsync(""DisplaySource""")) continue;
lines.Add(line);
}
}
return Trim(lines);
}
private IEnumerable<string> Trim(List<string> lines)
{
var contentLines = new List<string>();
int lastContentIndex = GetLastContentIndex(lines);
for (int i = 0; i <= lastContentIndex; i++)
{
string line = lines[i];
if (String.IsNullOrWhiteSpace(line) && contentLines.Count == 0) continue;
contentLines.Add(line);
}
return contentLines;
}
}
public class NullViewComponentResult : IViewComponentResult
{
public void Execute(ViewComponentContext context)
{
return;
}
public Task ExecuteAsync(ViewComponentContext context)
{
return Task.CompletedTask;
}
}
Todos
- ViewComponent ( MVC Core 1.1) TagHelpers
.
- .
- HtmlHelpers [ !; -)]
, .