Changing Form Fields / Model Design

Guillermo Perez 26 Reputation points
2021-02-04T23:43:30.877+00:00

Hello and sorry to bother you, I just have a question that has been on my mind for quite some time. In my mind I would like to develop a form to take ads, classified ads to be clear, so the basic model should include something like:

public class Ad{ public string Title{get;set;} public string Content{get;set;} public DateTime Expiration{get;set;} ... }

Now this works fine if all the ads are the same, now let's say I want to sell a car and an apple... both ads must have the Ad class already defined above, also the car would include something like Color, Engine Size, Pistons, Cylinders, etc... while the Apple would share only a single additional: Color, then add other properties not applicable to cars such as Variety, Sweetness, etc...

What I mean is:

APPLE
Title
Content
Expiration
Color
Sweetness
...

CAR
Title
Content
Expiration
Color
Cylinders
Horse Power
...

My question is... how can we create a form and model for such changing properties and sub-models...

for example, if I create a URL like https://localhost/CreateAd, what model should I use? I'm kind of lost :-( any help will be appreciated, thank you!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,211 Reputation points
    2021-02-05T01:23:18.147+00:00

    IMHO, you need to learn how to use a viewmodel that is strong type to a given view.

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    https://deviq.com/other/kinds-of-models

    You can have more than one model, a class in a viewmodel that is a class.

    You have a view for Apple a view for car, etc and etc.

    You can send in one viewmodel for all the views, but you know when you are using a view for apple so you configure the view for apple to use the apple model within the viewmodel. This thing about a base class a model in the viewmodel is suspect, and it would not pass a professional code review with each model having all the required properties for each model.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.