d
Not strictly only Umbraco but I am working in Umbraco in this case: I have a variable that creates an array that contains html which I then populate a list of things e.g. @Html.Raw(things) but the conditional logic has grown. I have now moved the logic in a foreach to the html (using viewbag) which is much nicer to work with and much less escaping! However, I know I should really move the logic to a controller and use a view model. Q - A: I understand that using viewbag may be less performant than have the logic inline. Is this correct and is there an alternative but still keeping the code in the cshaml file? Q - B: How do you decide when to move to full MVC - I do like the benefits of intellisense so should I always use a controller?
I currently have some inline logic in a var that generates an array containing html . With conditionals it has grown, so I am now using viewbag to pass it to my html which has a foreach which makes it much easier to edit. However, I am aware I 'could' move it to a controller. How do you decide when you 'should'? I understand the benefits.
s
I have 80 lines of business logic in a
Home.cshtml
and it definitely "shouldn't" be there. But this is a project that only I am going to use, so it is nothing that anybody else will encounter. If I feel like I need to make the code predictable to other people then I'll refactor. Huge assumption made that someone else would actually know the pattern though, it depends. So yeah, not really an answer 😅
d
Thanks @Sebastiaan - in this case the logic is duplicated in 5-6 more views so I think I most definitely should consider separation of concerns. I assume this pattern evolves with new requirements. I have so much to learn about backend best practice.
s
Oh yeah, things that repeat definitely need to move to a single place.. I "sin" sometimes but it always bites me. And on the other end, I've also overengineered a lot.. so it's a balance you need to find. Again, I have very little general advice, sorry! 😅
k
1. Why no razor intellisense? Not using VS? 2. "Always use a controller" takes away a huge benefit of Umbraco development, don't go there. Also not possible for blocks, right?
n
If you are looking to make things reusable/repeatable you aren't forced to use MVC, there are multiple options and depending what youa re trying to do may be better suited. 1. Partial View ? this is the simplest way, isolate it to a single partial that can be reused in different places 2. View Component? If you need access to some services or want to do the core logic in C# outside of a view then a View Component might be the right route, you'd still need a partial for output the result, but it might help you isolate your code if you want to?
Nothing is wrong with havaing logic code in your view file in my opinion, but it sounds like you have repeatiion which would lead me to do something reusable, based on what you've described I'd probably go view component.
d
I had forgotten about view components and had meant to look into them more - so that's my weekend studying sorted, thanks @Nik
h
Viewcomponents would be good here, I use them a lot, and generally pass a template name so the same component can render a different views of the same data
u
Another option would be to put the reusable logic in a class, and call the methods in that class. Apologies if that's what you meant by putting it in a controller, but a controller is a specific type of class.
s
Smells like you need a view component? EDIT: Doh - seems like discord isn't updating for me - this has already been suggested!
3 Views