tag-helpers

MVC6 TagHelpers with disposable

匆匆过客 提交于 2019-12-01 06:40:38
In the older MVC HTML Helpers, one could use IDisposable to wrap content - for example the BeginForm helper would automatically wrap *stuff* with a closing form tag <% using (Html.BeginForm()) {%> *stuff* <% } %> Is this wrapping of content supported with MVC6 TagHelpers? For example I would like this <widget-box title="My Title">Yay for content!</widget-box> to be expanded into a bootstrap widget-box with wrapping divs: <div class="widget-box"> <div class="widget-header"> <h4 class="widget-title">My Title</h4> </div> <div class="widget-body"> <div class="widget-main"> Yay for content! </div>

Tag Helpers - Self closing HTML tags is a bad habit?

只谈情不闲聊 提交于 2019-12-01 06:37:41
I am incorporating TagHelpers in my MVC vNext project, and I realized they don't work when I self close the HTML tags. @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers" <label asp-for="FirstName"/> <!-- self closing tag --> <span asp-validation-for="FirstName"/> <!-- self closing tag --> And, insteand when I put the closing tags, I see the values being displayed. <label asp-for="FirstName"></label> <span asp-validation-for="FirstName"></span> Generated HTML <label for="FirstName">FirstName</label> <span class="field-validation-error" data-valmsg-for="FirstName" data-valmsg-replace="true">

asp.net core tag helper for conditionally add class to an element

心已入冬 提交于 2019-12-01 04:30:42
In Asp.Net MVC we can add class conditionally as following code: <div class="choice @(Model.Active?"active":"")"> </div> How can do this by using tagHelper and by remove else part in condition. Ability to add a conditional css class by following tagHelper provides. this code like AnchorTagHelper asp-route-* for add route values acts. [HtmlTargetElement("div", Attributes = ClassPrefix + "*")] public class ConditionClassTagHelper : TagHelper { private const string ClassPrefix = "condition-class-"; [HtmlAttributeName("class")] public string CssClass { get; set; } private IDictionary<string, bool>

MVC6 TagHelpers with disposable

故事扮演 提交于 2019-12-01 04:09:35
问题 In the older MVC HTML Helpers, one could use IDisposable to wrap content - for example the BeginForm helper would automatically wrap *stuff* with a closing form tag <% using (Html.BeginForm()) {%> *stuff* <% } %> Is this wrapping of content supported with MVC6 TagHelpers? For example I would like this <widget-box title="My Title">Yay for content!</widget-box> to be expanded into a bootstrap widget-box with wrapping divs: <div class="widget-box"> <div class="widget-header"> <h4 class="widget

asp.net core tag helper for conditionally add class to an element

守給你的承諾、 提交于 2019-12-01 01:32:36
问题 In Asp.Net MVC we can add class conditionally as following code: <div class="choice @(Model.Active?"active":"")"> </div> How can do this by using tagHelper and by remove else part in condition. 回答1: Ability to add a conditional css class by following tagHelper provides. this code like AnchorTagHelper asp-route-* for add route values acts. [HtmlTargetElement("div", Attributes = ClassPrefix + "*")] public class ConditionClassTagHelper : TagHelper { private const string ClassPrefix = "condition

TagHelpers add custom class for LabelTagHelper based on validation attribute [Required]

三世轮回 提交于 2019-12-01 01:29:14
In Core MVC there is anew concept as Tag helpers. We could previously create custom html helpers to attach some classes based on the validation data annotations such as [Required]. As TagHelpers arq quite new area I cannot find anough resources to achieve the following: here is the view model: [Required] public Gender Gender { get; set; } view: <label class="control-label col-md-3 required" asp-for="Gender"></label> css: .required:after { content: "*"; font-weight: bold; color: red; } output: But I don't want to manully add the required css class in the label. Somehow I shoudl be able to

Can I use a Tag Helper in a custom Tag Helper that returns html?

房东的猫 提交于 2019-11-30 11:03:30
I recently ran into a situation where I would like to use a tag helper within a tag helper. I looked around and couldn't find anyone else trying to do this, am I using a poor convention or am I missing documentation? Ex. Tag Helper A outputs HTML that contains another tag helper. Ex. [HtmlTargetElement("tag-name")] public class RazorTagHelper : TagHelper { public override void Process(TagHelperContext context, TagHelperOutput output) { StringBuilder sb = new StringBuilder(); sb.Append("<a asp-action=\"Home\" "); output.Content.SetHtmlContent(sb.ToString()); } } Is there a way for me to process

MVC 6 Tag Helpers and foreach

爱⌒轻易说出口 提交于 2019-11-30 08:04:51
What would I give to asp-for property of a label tag helper in order to display items from a collection. The code below generates a compilation error. @foreach (var item in Model) { <label asp-for="item.BookingCode"></label> } The @ character escapes the default model lambda code. Therefore you can type: @foreach (var item in Model) { <label asp-for="@item.BookingCode"></label> } user2800307 I Have a simple way to do a list and show properties of it. List<string> razones = new List<string>(); foreach (var item in _context.Reason) { razones.Add (item.Description); } System.Diagnostics.Debug

Radio Button Tag Helpers in ASP.NET 5 MVC 6

让人想犯罪 __ 提交于 2019-11-30 06:01:17
I don't see any tag helpers for radio buttons in ASP.NET 5 MVC 6. What's the right way of handling form elements where I need to use radio buttons? There is a TagHelper for all the input types which includes the radio button type as well. Assuming you have a view model like this public class CreateUserVm { public string UserName { set; get; } public IEnumerable<RoleVm> Roles { set; get; } public int SelectedRole { set; get; } } public class RoleVm { public int Id { set; get; } public string RoleName { set; get; } } And in your GET action, public IActionResult Index() { var vm = new

Append QueryString to href in asp.net core Anchor Helper Tag

夙愿已清 提交于 2019-11-29 14:04:36
I am trying to add anything in the query of the request to anchors in the html result: Fictitious example: User makes a request (note that band and song could be anything, I have a route catering this request: template: "{band}/{song}"): http://mydomain/band/song?Param1=111&Param2=222 Now I want my anchors to append the query string part to the href of my anchors. So I tried something like this (note the 'asp-all-route-data'): <a asp-controller="topic" asp-action="topic" asp-route-band="iron-maiden" asp-route-song="run-to-the-hills" asp-all-route-data="@Context.Request.Query.ToDictionary(d=>d