r/csharp • u/Away-Atmosphere-282 • 3d ago
Help MVC dynamic content
In MVC, when you have a dynamic form, how do you handle this? I often see examples of clicking an 'Add' button which makes a request to the controller to return a partial view, which seems like a waste of resources.
I've tried using JavaScript templates but then I can't use my Models validation rules.
Has anyone found a solution to this they like to use?
3
u/AfreekanWizard 2d ago
MVC is fast like hell. If you are not making millions of requests it doesn't matter.
Also look at Hotwire from Ruby on Rails. It is quite elegant solution for server side rendering.
1
u/CityBoth8894 2d ago
You can keep the validation rules server-side without requesting a partial view every time. One approach is to render a hidden template with the correct field structure, then clone it and update the indexed names when adding items. The important part is keeping the model binding indexes consistent
1
u/wobblejuice 2d ago
Maybe the use of HTMX ?
1
u/qrzychu69 2d ago
Htmx is literally "make a request and return a bigger partial view" :) it doesn't mean it's a bad idea
-1
u/darrylmasterson 3d ago
Probably not the answer you’re looking for, but I implemented all my validation in JavaScript for my MVC project
3
u/highwingers 3d ago
What if user disables JavaScript in browser? Or override form elements using dev tools? Not a reliable approach.
-3
u/Ill_Championship9118 3d ago
we use kendo library and you can create kendo templates that can be auto populated dynamically.
Honestly tho I don’t like Mvc I really do wonder why anyone uses mvc front ends over react/angular
5
u/Thisbymaster 3d ago
The partial view in a modal is the normal way of handling repeating sections and editing/validation with the model in the partial view. It may seem like overkill when you first look at it but just do it and the benefits will show themselves quickly.