Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

ViewComponents as TagHelpers with optional parameters #5541

Closed
svallis opened this issue Nov 17, 2016 · 5 comments
Closed

ViewComponents as TagHelpers with optional parameters #5541

svallis opened this issue Nov 17, 2016 · 5 comments

Comments

@svallis
Copy link

svallis commented Nov 17, 2016

The new TagHelper syntax for ViewComponents looks great, but it seems that it'll only match the attributes you specify exactly to those on your Invoke() or InvokeAsync() method signature. Is that correct? It makes any kind of optional parameters impossible as far as I can see.

So with an example ViewComponent:

public class TestViewComponent : ViewComponent
{
    public IViewComponentResult Invoke(bool showFooter = false)
    {
        return View(showFooter);
    }
}

Either of the following work correctly:

<vc:test show-footer="false"></vc:test>
<vc:test show-footer="true" />

However either of the following are output directly to the HTML unprocessed:

<vc:test />
<vc:test></vc:test>

It's not possible to overload the Invoke() methods and the default values for parameters will never be used. In a more complex example than above you can see that adding a new optional parameter to a ViewComponent will cause it to silently fail anywhere that the attributes and signatures do not match, writing server side code to the browser.

@NTaylorMullen
Copy link
Contributor

In our initial prototypes we supported this but removed support due to holes in our implementation. @ajaybhargavb we can support this when we move ViewComponent TagHelper attribute checks to be at runtime.

@shanselman
Copy link

@NTaylorMullen So was this given up on?

@NTaylorMullen
Copy link
Contributor

@shanselman We've lately been more aggressive in backlogging/closing items. Given that it's not closed we haven't given up on it yet. It just means it hasn't yet fit into any of the currently planned milestones.

Lots of pieces moving currently! 😄

@eallegretta
Copy link

I've spent like 3 hours trying to figure out why on earth my ViewComponent was not working until I created a new project and found out the problem was the optional parameter.

The documentation found here SHOULD be updated to reflect that

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components

@NTaylorMullen NTaylorMullen removed this from the Backlog milestone Jun 29, 2017
@NTaylorMullen
Copy link
Contributor

NTaylorMullen commented Jun 29, 2017

Duplicate/tracked in different repo: https://github.com/aspnet/Razor/issues/1266

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

No branches or pull requests

6 participants