简体   繁体   English

Razor 页面中的“条件”字段

[英]"Conditional" Fields in Razor Pages

Im making a ASP NET CORE Razor Pages project and I have the this class我正在制作一个 ASP NET CORE Razor Pages 项目,我有这个类

MyObject.cs 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;我有 5 种不同类型的“MyObjects”,每一种都有不同的属性组合; for instance, "MyObject_ASD" use the Id, Name, Val01, Val02, Val05;例如,“MyObject_ASD”使用Id、Name、Val01、Val02、Val05; but "MyObject_QWE" only use Id, Name, Val11, Val13, Val17.但“MyObject_QWE”只使用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"我需要知道实现页面的最佳/最佳方式是什么,只显示所选“MyObject”的字段

PS聚苯乙烯

In a MVC approach I use five _Create.cshtml views, like _CreateASD.cshtml, _CreateQWE.cshtml with a switch-case in the controller.在 MVC 方法中,我使用五个 _Create.cshtml 视图,例如 _CreateASD.cshtml、_CreateQWE.cshtml,在控制器中带有一个 switch-case。

            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.甚至你可以创建一个 enums 属性来区分你的模态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM