Im making a ASP NET CORE Razor Pages project and I have the this class
MyObject.cs
public class MyObject
{
public int Id { get; set; }
public string Name { get; set; }
public string Val01 { get; set; }
public string Val02 { get; set; }
//...
public string Val30 { get; set; }
}
And I have 5 different types of "MyObjects" each one have different combinations of properties; for instance, "MyObject_ASD" use the Id, Name, Val01, Val02, Val05; but "MyObject_QWE" only use Id, Name, Val11, Val13, Val17.
I need to know what is the best/optimal way to implement a page shows only the fields of the selected "MyObject"
PS
In a MVC approach I use five _Create.cshtml views, like _CreateASD.cshtml, _CreateQWE.cshtml with a switch-case in the controller.
switch (type)
{
case MyObjectType.QWE:
return PartialView("_CreateQWE", object);
case MyObjectType.ASD:
return PartialView("_CreateASD", object);
case MyObjectType.ZXC:
return PartialView("_CreateZXC", object);
}
For me, if each object has different properties, they must be different objects. If the objects has many common properties, they must has a mother class. I agree with your approach and each object has your own view. It's just my opinion, but i don't know the context to make sure.
I suggest you use inheritance class to create multiple similar class.
It's make your code clean and more readable and easy to control.
In your case, for example :
Create a base class, and other class inherit this base.
Controller :
// base class
public class MyObject
{
public int Id { get; set; }
public string Name { get; set; }
}
// derived class
public class MyObjectA : MyObject
{
public string Val01 { get; set; }
public string Val02 { get; set; }
}
public class MyObjectB : MyObject
{
public string Val03 { get; set; }
public string Val04 { get; set; }
}
public class MyObjectC : MyObject
{
public string Val05 { get; set; }
public string Val06 { get; set; }
}
View :
switch (type)
{
case MyObjectA :
return PartialView("_CreateQWE", MyObjectA );
case MyObjectB :
return PartialView("_CreateASD", MyObjectB );
case MyObjectC :
return PartialView("_CreateZXC", MyObjectC );
}
or even you can create a enums property to distinguish your modals.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.