【问题标题】:Handle several view models int ASP.NET MVC 5处理多个视图模型 int ASP.NET MVC 5
【发布时间】:2015-01-28 01:07:31
【问题描述】:

我正在开发一个应用程序,该应用程序应该通过不同的活动评估一组学生。

一个活动有一个名称、一个描述、一些其他属性和一组问题,如“真假问题”、“多选题”、 “单选题”、“顺序题”等。

我正在使用 ASP.NET MVC 5、实体框架(代码优先)和 C#。

我的问题是处理不同视图模型的更好方法是什么?

事实上,问题类型可能会随着时间的推移而增加,并且这些类型可能会有很大差异,例如拖放、排序、对错、单选、多选。

我的第一个方法是:

(1) 为每个活动创建一个具有公共属性的抽象类

abstract class ActivityViewModel {

    // Some cool properties like name, description, modification date time, .....
}

(2) 创建多个视图模型,每个模型都有一组不同的问题类型

class TrueOrFalseActivityViewModel : ActivityViewModel {

    ICollection<TrueOrFalseQuestion> myCoolQuestions;
}

class OrderActivityViewModel : ActivityViewModel {

    ICollection<OrderQuestion> myCoolQuestions;
}

(3) 为每个视图模型创建一个视图和一个以 ActivityViewModel 作为参数的 Create Action。

问题是asp.net mvc控制器中的post action不能处理抽象类。

我应该使用吗?

(1) 具有多个操作的单个控制器:

class MySuperController : Controller {

    // Get actions lies here

    [HttpPost]
    public ActionResult Create(TrueQuestionViewModel viewModel) {
        // Some cool actions here

    }

    [HttpPost]
    public ActionResult Create(SingleAnswerViewModel viewModel) {
        // Some cool actions here

    }

    [HttpPost]
    public ActionResult Create(MultipleAnswerViewModel viewModel) {
        // Some cool actions here
    }

}

(2) 多个控制器,每个活动一个,具体取决于其包含的问题类型。

class MyTrueOrFalseActivityController : Controller  {

        public ActionResult Create(TrueQuestionActivityViewModel viewModel) {
            // Some cool actions here
        }

        public ActionResult Update(TrueQuestionActivityViewModel viewModel) {
            // Some cool actions here
        }

        // Several actions here..... 
    }

class MyOrderActivityController : Controller  {

        public ActionResult Create(OrderActivityViewModel viewModel) {
            // Some cool actions here
        }

        public ActionResult Update(OrderActivityViewModel viewModel) {
            // Some cool actions here
        }

        // Several actions here..... 
    }

我需要建议以支持代码重用、使事情尽可能简单并处理传入的问题类型

【问题讨论】:

  • 如果一个活动可以包含TrueOrFalseQuestionSingleAnswer等的集合,那么您应该有一个ActivityViewModel,其中包含Activity的属性和List&lt;TrueOrFalseQuestionViewModel&gt;List&lt;SingleAnswerViewModel&gt;等的属性. 一种视图模型,一种 GET 方法和一种 POST 方法。

标签: asp.net asp.net-mvc viewmodel


【解决方案1】:

你可以试试这个:

拥有一个可以存储所有可能答案类型的视图模型。您可以创建接口来缩小您希望在操作方法中最重要的问题/答案的类型。例如

    public interface ITrueFalseQuestion
    {
        bool Answer1 { get;set; }
        bool Answer2 { get;set; }
    }

public class ActivityViewModel : ITrueFalseQuestion
{
    bool Answer1 { get;set; }
    bool Answer2 { get;set; }
}

因此在控制器中你可以这样做:

[HttpPost]
public ActionResult Question(ActivityViewModel question)
{
    var g = question as ITrueFalseQuestion;
    g.Answer1 // Do stuff....
}

我认为这种方法可能适合您,具体取决于 ActivityViewModel 最终会变成多大,

【讨论】:

    猜你喜欢
    • 2014-06-25
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多