Use View Components, not Partial Views, in ASP.NET Core
ฝัง
- เผยแพร่เมื่อ 22 พ.ย. 2024
- Check out the full article on the Telerik bog associated with this video: www.telerik.co...
Author: Peter Kellner ( peterkellner.net )
Source available on GitHub: github.com/pke...
Why use View Components and not Partial Views? The biggest reason is that when inserting a Partial View into a Razor page, all the ViewData associated with the calling View is automatically associated with the Partial View. This means that a Partial View may behave very differently on one Razor page than on another. With View Components, you control what gets shared to your View Components.
Excellent explanation. Thanks to that I decided to buy Peter's course about ASP.NET Core Tag Helpers and View Components on Pluralsight. I recommend.
Thanks Peter! I was looking around for a simple explanation of view component in Razor and YT land this in my search... lucky me 🙂
Excellent video and very helpful. It's fantastic that it has an accompanying blog + git repository. Well done, thank you!
Nice explanation
Thanks and
Greeting from the desert
Thanks! Which desert? I spend most my time now in Borrego Springs (desert community).
This works great! I agree it is nicely self-contained and easy to work with. Thank you!
Thanks for this video, very succinct!
AARGH!!! I have been driving myself crazy! I am new to Razor Pages and am migrating a large application that extensively used nested ascx pages where a nested page would call a second nested page while passing objects forward. I can see no way of getting OnGet() on a nested view or Razor Page to be called, nor can I pass a variable or even call a child method from a parent method. Please can you spread some light on this?
Thank you Peter , excelent video.
Thanks Peter!
So, ViewComponent is killer because encapsulation and no side effects...
If that's supposed to be convincing, then what's an example of a side effect in a partial view?
how can you refresh a view component using javascript
This makes no damn sense if you still have to use js.... whats the difference between using an ajax request and hitting a controller and using a partial view and then injecting the returned html into the proper dom element?
can I somehow create a component thats for example, something like
and on that page when i change the value of variable, it hits the view component and loads what it needs to that way... or does this still require js?
Good info + Code, great!
Thanks!
Other than being 'invoked' automatically, this video doesn't explain fully why a partial view cannot be used in place of another one of those MS additional-useless-feature called ViewComponent. If Microsoft developed what they think now is a useless partial view, now they're using another soon-to-be useless ViewComponent.
Using View Components everywhere is overkill and completely unnecessary. They each have their correct situational usage. Using either one everywhere is just wrong.
Why not use BLAZOR ??? it does exactly this with much ease!
You could use blazor, just like you can pretty much use any tech you want. Blazor is just another tech that gives us the same result but goes about it very differently.