我如何使用一个视图来编辑具有共同基类的 2 个不同类?

发布于 2025-01-07 14:43:10 字数 938 浏览 2 评论 0原文

好吧,我需要创建两份几乎相等的调查问卷。区别在于一个人比另一个人有更多的问题。所以我创建了一个只有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

银河中√捞星星 2025-01-14 14:43:10

我可能会稍微切换模型:

public class QuestionClass
{
  public String Question { get; set; }

  public String Answer { get; set; }
}

public class QuizzClass
{
  public Int32 ID { get; set; }

  [UIHint("Question")]
  public IList<QuestionClass> Questions { get; set; }
}

然后为问题创建一个视图,而不是使用对象的“键/值”设置来使用 [DisplayAttribute]

// ~/Views/_controller_/_action_.cshtml
@model QuizzClass

@using (Html.BeginForm())
{
  for (var q = 0; q < Model.Questions.Length; q++)
  {
    // ~/Views/Shared/EditorTemplates/Question.cshtml
    @Html.EditorFor(x => Model.Questions[q]);
  }
}

拥有如此多的相似性,却明确地指出“不同”对象中的每个问题,这似乎有点过分了。

I would probably switch the model up a bit:

public class QuestionClass
{
  public String Question { get; set; }

  public String Answer { get; set; }
}

public class QuizzClass
{
  public Int32 ID { get; set; }

  [UIHint("Question")]
  public IList<QuestionClass> Questions { get; set; }
}

Then create a view for the question, and instead of using the [DisplayAttribute] using the "key/value" setup of the object.

// ~/Views/_controller_/_action_.cshtml
@model QuizzClass

@using (Html.BeginForm())
{
  for (var q = 0; q < Model.Questions.Length; q++)
  {
    // ~/Views/Shared/EditorTemplates/Question.cshtml
    @Html.EditorFor(x => Model.Questions[q]);
  }
}

It just appears over-kill to have that much similarity, yet explicitly calling out each question in your "different" objects.

月亮坠入山谷 2025-01-14 14:43:10

我认为你的架构有问题。为什么不直接开设一个包含您的问题集合的课程呢?

I think your architecture is faulty. Why not just have a class with a collection that holds your questions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文