我如何使用一个视图来编辑具有共同基类的 2 个不同类?
好吧,我需要创建两份几乎相等的调查问卷。区别在于一个人比另一个人有更多的问题。所以我创建了一个只有 ID(持久性)的类作为两者的基类:
public class BaseQuizzClass{
public int ID {get;set;}
}
然后我创建了这些类,变量的名称几乎相同(我认为这可以帮助使用 razor):
public class Quizz1 : BaseQuizzClass{
[Display(Name="QuestionHere")]
public string q1 {get;set;}
[Display(Name="QuestionHere")]
public string q2 {get;set;}
...
[Display(Name="QuestionHere")]
public string q9 {get;set;}
}
public class Quizz2 : BaseQuizzClass{
[Display(Name="QuestionHere")]
public string q1 {get;set;}
[Display(Name="QuestionHere")]
public string q2 {get;set;}
...
[Display(Name="QuestionHere")]
public string q9 {get;set;}
[Display(Name="QuestionHere")]
public string q10 {get;set;}
}
然后我创建了使用 BaseQuizzClass 作为模型的视图,因此我可以将子类作为参数传递给它。但我不知道如何访问子类属性。
OBS.:每个问题都有自己的文本,在显示注释中定义。
有什么办法可以做我想做的事吗? (我不太确定我是否清楚)
Well, I need to create two questionnaires that are almost equals. The difference are that one have a question more than the other. So I created a class with only the ID (to persistence) to be the base class for both:
public class BaseQuizzClass{
public int ID {get;set;}
}
And then I created the classes, with almost the same names for the variables ( I thought that it could help using razor):
public class Quizz1 : BaseQuizzClass{
[Display(Name="QuestionHere")]
public string q1 {get;set;}
[Display(Name="QuestionHere")]
public string q2 {get;set;}
...
[Display(Name="QuestionHere")]
public string q9 {get;set;}
}
public class Quizz2 : BaseQuizzClass{
[Display(Name="QuestionHere")]
public string q1 {get;set;}
[Display(Name="QuestionHere")]
public string q2 {get;set;}
...
[Display(Name="QuestionHere")]
public string q9 {get;set;}
[Display(Name="QuestionHere")]
public string q10 {get;set;}
}
Then I created a View using the BaseQuizzClass as model, so I could pass an child class as parameter toit. But I don't know how to access the childs class attributes.
OBS.: Each question has your own text, defined in the Display annotation.
There's any way to do what I want? (I'm not pretty sure if I was clear)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会稍微切换模型:
然后为问题创建一个视图,而不是使用对象的“键/值”设置来使用
[DisplayAttribute]
。拥有如此多的相似性,却明确地指出“不同”对象中的每个问题,这似乎有点过分了。
I would probably switch the model up a bit:
Then create a view for the question, and instead of using the
[DisplayAttribute]
using the "key/value" setup of the object.It just appears over-kill to have that much similarity, yet explicitly calling out each question in your "different" objects.
我认为你的架构有问题。为什么不直接开设一个包含您的问题集合的课程呢?
I think your architecture is faulty. Why not just have a class with a collection that holds your questions?