Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic MVC Views

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?

like image 796
John Mitchell Avatar asked Aug 27 '12 18:08

John Mitchell


People also ask

What is view binding in MVC?

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.

What is @model in Cshtml?

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.

What is ViewModel in MVC with example?

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.

What is @model in C#?

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.


1 Answers

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.

like image 118
Display Name Avatar answered Sep 23 '22 20:09

Display Name