So I am making a registration page in ASP.NET MVC and theres a list of fields which changes depending on a number of conditions (taken from a database).
What would be considered the best approach for displaying these fields on a view without code changes each time the DB is updated.
The current system I am thinking about is making a Dictionary with "Name, DataType" then just iterating through the list and putting a new input type of each name (using a switch for data type to get the correct editor). But it seems like a old fashioned and not fully utilizing the benefits of the .NET MVC system (such as validation, default values, customized editors for specific content).
Is there a approach I may possibly be not aware of in .NET to handle this problem?
Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.
This @model directive allows you to access the movie that the controller passed to the view by using a Model object that's strongly typed. For example, in the Details. cshtml template, the code passes each movie field to the DisplayNameFor and DisplayFor HTML Helpers with the strongly typed Model object.
View Model is a model class that can hold only those properties that are required for a view. It can also contain properties from more than one entity (tables) of the database. As the name suggests, this model is created specifically for the View requirements.
In MVC M stands for Model and Model is a normal C# class. Model is responsible for handling data and business logic. A model represents the shape of the data. Model is responsible for handling database related changes.
I had very similar problem. Posting is here.
No, there is no polymorphism views in ASP.NET MVC. At least I couldn't make it work doing tons and tons of experimentation and asking this question several times in different ways on Stack Overflow.
The type of @model
defined in the view will define its type inside the view, rather than actual type of the model (in case it was interface or abstract class or base class, while you passing in child class with all your beautiful decorations, which won't work, since type is defined by model type).
Sorry, no positive answer to your question. I ended up writing bunch of editor templates, which were only different by model type inside. Huge DRY violation, but this is the way ASP MVC worked, unfortunately.
Hope this helps saving you some time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With