asked on
In a .NET Core 5 application, how do I dynamically build and change a View *.cshtml page's layout?
In a .NET Core 5 application, how do I dynamically build and change a View *.cshtml page's layout?
I know how to get data in the Controller code, add it to a ViewData["xxx"] and use that data when the View builds the first time, but I don't know how to change the layout.
For example, I have a table that needs to have rows added based on the user's info.
If this were a JavaScript / HTML site, I could make an .ajax call in the JS and build the table dynamically.
How can I dynamically change a *.cshtml View in .NET Core?
Do I send data to the View through a ViewData[*] and use C# to do that?
For example, if the table needs to have three rows with different data for each row, would I put all that in a ViewData[*] and then use that info to build the table within the View?
More importantly, what if I need the layout to change based on user action?
E.g. what if every time a user clicks a button, another row is added to a table. This would be a row that includes input fields.
If not, you can use a partial view, as explained in that same page, or you can make an ajax call and use Javascript.
Once the page is loaded, *almost* everything is done via AJAXSame here.
Same here.That's the way :) I guess...
ASKER
https://www.aspsnippets.com/Articles/Dynamically-add-rows-to-HTML-Table-in-ASPNet-MVC.aspx
But I had a difficult time believing that with all the bells and whistles MVC supposedly offers, they didn't have a more managed code sort of option. With the feedback here, I see that JS / .ajax is indeed the correct way to go.
I feel like asp.net is going back to the way it was a long time ago except now there's an action method sending data to each HTML page through behind-the-scenes routing.
I've had a horrible time with Angular. I hope people stop using that. I've heard great things about Blazor and will eventually adopt that.
And you are right about Angular (I'd dare say even React) and we will know in couple of years whether we are right or wrong about Blazor but it is exactly what we want: A server side tech manipulating client side in an easy to implement, debug and troubleshoot way along with highest possible level of efficiency and security. :P
PS: Now I feel like a sales guy pitching Blazor.
PPS: No... I am not. :D
ASKER
https://www.c-sharpcorner.com/UploadFile/17e8f6/how-to-display-dynamic-content-inside-views/
As it turns out, that tutorial doesn't explain how to change layout after a page is loaded.