Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TagHelpers #44

Closed
vkichline opened this issue Oct 14, 2015 · 17 comments
Closed

TagHelpers #44

vkichline opened this issue Oct 14, 2015 · 17 comments

Comments

@vkichline
Copy link
Contributor

It would be a big help to introduce TagHelpers here and use them instead of HTML Helpers. This walkthrough doesn't prepare me to understand the Web Application template, which uses TagHelpers exclusively.

@bricelam
Copy link
Contributor

The tag helper documentation lives on the ASP.NET doc site.

@rowanmiller
Copy link
Contributor

I think this refers to the Getting Started with ASP.NET 5 article we have. I'm personally not a fan of tag helpers, which is probably why they aren't used in the tutorial... but I agree being consistent with the template is a good idea.

@Eilon
Copy link
Member

Eilon commented Oct 14, 2015

Should consult with @DamianEdwards on the best practices here.

@Rick-Anderson
Copy link
Contributor

http://docs.asp.net/projects/mvc/en/latest/views/tag-helpers/authoring.html
http://docs.asp.net/projects/mvc/en/latest/views/tag-helpers/intro.html
Early feedback on my two TH articles is great. Run through my TH tutorials and I'm sure you'll love them.

@rowanmiller
Copy link
Contributor

I have no problem adjusting the article to use tag helpers, that is definitely the right thing to do from a consistency point of view. I'm about to update everything to Beta 8 so I will do that at the same time.

Regarding liking tag helpers... for me personally it makes it too hard to distinguish between code and html. The thing I love about razor is that it's really clear what is code and it's just the same C# I write everywhere else in my application. With tag helpers that is completely obscured away.

@Rick-Anderson
Copy link
Contributor

Read my intro doc - if you're using the default or light theme, TH/C# code is in a bold purple font. If you’re using the “Dark” theme the font is bold teal. I won't repeat the other advantages here, but I'm confident after using TH you'll grow to love them.

@rowanmiller
Copy link
Contributor

GitHub doesn't know about them so I lose all that context when I'm looking at diffs, PR's etc. That's not my main issue though, if I look at this piece of markup... I have no idea what it's doing. If it was just calling APIs though I could go look at them, read Intellisense, etc.

        <environment names="Development">
            <link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css" />
            <link rel="stylesheet" href="~/lib/bootstrap-touch-carousel/css/bootstrap-touch-carousel.css" />
            <link rel="stylesheet" href="~/css/site.css" />
        </environment>

BTW I'm not saying they don't provide value and that there aren't a bunch of folks who will like them... I'm just not one of them. I just want C# code and no magic/abstractions.

@rowanmiller
Copy link
Contributor

@Rick-Anderson quick question, how do use the asp-for when I accessing something in a for loop? It looks like the default scope is to try and access the supplied item from Model but in this case I'm enumerating over the results of the model.

Here is the code, see where I'm trying to replace @Html.DisplayFor(modelItem => item.BlogId) with a tag helper.

    @foreach (var item in Model)
    {
        <tr>
            <td>
                <label asp-for="BlogId" />
                @Html.DisplayFor(modelItem => item.BlogId)
            </td>
            <td>
                @Html.DisplayFor(model => item.Url)
            </td>
        </tr>
    }

@Rick-Anderson
Copy link
Contributor

@NTaylorMullen @rynowak can you answer @rowanmiller question?

@NTaylorMullen
Copy link

@rowanmiller You can do: <label asp-for="@item.BlogId"></label>

@Rick-Anderson Rick-Anderson reopened this Oct 15, 2015
@Rick-Anderson
Copy link
Contributor

@NTaylorMullen should that go in authoring TH or advanced?

@DamianEdwards
Copy link
Member

You put an @ in front of the expression, then you can point it to whatever you like: asp-for="@item.BlogId"

@NTaylorMullen
Copy link

@Rick-Anderson Not authoring, it's more of a usage behavior for ModelExpression properties which are used by Mvc TagHelpers.

@dougbu
Copy link
Member

dougbu commented Oct 15, 2015

We should not recommend foreach with expressions such as @item.BlogId or lambdas such as model => item.Url in general. They work fine in this display-only scenario but a generated <input> or <label> will have incorrect id or for attributes (respectively) and won't be bound correctly on submission. Need to mess with ViewData.TemplateInfo.HtmlFieldPrefix when using foreach. That's less straightforward than:

    @for (var i = 0; i < Model.Count; i++)
    {
        <tr>
            <td>
                <label asp-for="@Model[i].BlogId" />
                @Html.DisplayFor(model => model[i].BlogId)
            </td>
            <td>
                @Html.DisplayFor(model => model[i].Url)
            </td>
        </tr>
    }

@rowanmiller
Copy link
Contributor

Yeah the @ prefix didn't work for me either, it just displayed blank data.

It's also super weird because anywhere else @item.BlogId within markup would be resolved to its actual value by running the code and then that value used in the tag. Now I need to know that @ behaves differently depending on whether it's in a normal HTML tag or a Tag Helper.

I've swapped over to tag helpers everywhere else in #45. I'll leave this section as-is for the moment.

@dougbu
Copy link
Member

dougbu commented Oct 15, 2015

@rowanmiller @item.BlogId is still resolved to the expression's "actual value", just a bit later.

As soon as you type @ anywhere, Razor puts you in a C# context. The difference w/ asp-for is that you're already in a C# context, after someLambdaParameter => someLambdaParameter. (note the trailing period). After the @, you're in a different C# context -- instead after someLambdaParameter =>.

@rowanmiller
Copy link
Contributor

This is done in 3eba324, aside from the for loop scenario being discussed here (where it sounds like tag helpers are not the right thing to use).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants