【问题标题】:How to design a ViewModel for a todo list application?如何为待办事项列表应用程序设计 ViewModel?
【发布时间】:2012-02-10 15:52:33
【问题描述】:

我正在创建一个简单的待办事项应用程序,它有两个实体,taskscategories

要创建task,必须选择category。为此,我想我需要一个 ViewModel。

这里是任务实体

public class Task
{
    public int taskId { get; set; }
    public int categoryId { get; set; }
    public string taskName { get; set; }
    public bool isCompleted { get; set; }
    public DateTime creationDate { get; set; }
    public DateTime completionDate { get; set; }
    public string remarks { get; set; }
    public string completionRemarks { get; set; }
}

这是类别实体

public class Category
{
    public int categoryId { get; set; }
    public string categoryName { get; set; }
}

如何设计TaskCategoryViewModel,以便在CreateTask 视图中绑定category

编辑:我使用的是经典的 ADO.NET,而不是 Entity Framework 或 LINQ to SQL。

【问题讨论】:

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


    【解决方案1】:

    基绍尔,

    最好的办法是拥有为您的任务和类别定义定义的模型(多合一)

    这就是所有东西的结合方式。

    在哪里

    IEnumerable<SelectListItem> Categories
    

    用于创建可以使用的下拉列表

    <%= Html.DropDownListFor(model=>model.NewTask.categoryId, Model.Categories) %>
    

    这将为您创建漂亮的下拉列表

        private IEnumerable<Category> GetCategories
        {
            get
            {
                List<Category> categories = new List<Category>
                                                {
                                                    new Category() {categoryId = 1, categoryName = "test1"},
                                                    new Category() {categoryId = 2, categoryName = "category2"}
                                                };
                return categories;
            }
        }
    
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult CreateTask()
        {
            TaskModel taskModel = new TaskModel();
            LoadCategoriesForModel(taskModel);
            return View(taskModel);
        }
    
        private void LoadCategoriesForModel(TaskModel taskModel)
        {
            taskModel.Categories =
                GetCategories.Select(
                    x =>
                    new SelectListItem()
                        {Text = x.categoryName, Value = x.categoryId.ToString(CultureInfo.InvariantCulture)});
        }
    
        public ActionResult CreateTask(TaskModel taskModel)
        {
            if (ModelState.IsValid)
            {
                // do your logic for saving
                return RedirectToAction("Index");
            }
            else
            {
                LoadCategoriesForModel(taskModel);
                return View(taskModel);
            }
        }
    
        /// <summary>
        /// your model for creation
        /// </summary>
        public class TaskModel
        {
            public Task NewTask { get; set; }
            public IEnumerable<SelectListItem> Categories { get; set; }
        }
    
        /// <summary>
        /// Task
        /// </summary>
        public class Task
        {
            public int taskId { get; set; }
            public int categoryId { get; set; }
            public string taskName { get; set; }
            public bool isCompleted { get; set; }
            public DateTime creationDate { get; set; }
            public DateTime completionDate { get; set; }
            public string remarks { get; set; }
            public string completionRemarks { get; set; }
        }
    
        /// <summary>
        /// Category
        /// </summary>
        public class Category
        {
            public int categoryId { get; set; }
            public string categoryName { get; set; }
        }
    

    【讨论】:

      【解决方案2】:

      在 TaskViewModel(我更喜欢将其命名为 CreateTaskViewModel)中,为类别选择列表创建属性

      public IEnumerable<SelectListItem> CategoriesSelectList;
      

      在控制器中,在返回视图之前绑定该属性(注意,这也应该在后处理程序中完成,当 ModelState 无效时)

      public ViewResult Create()
      {
           CreateTaskViewModel  model = new CreateTaskViewModel();
           model.CategoriesSelectList = _repository.AllCategories().Select(x=> new SelectListItem(){ Text = x.CategoryName, Value = x.CategoryId.ToString();}
      }
      

      最后,在视图中

      Html.DropDownListFor(model => model.CategoryId, Model.CategoriesSelectList)
      

      编辑:

      在您的代码中,_repository.AllCategories() 应替换为您的数据访问代码,该代码返回类型为 IEnumerable&lt;Category&gt; 的对象。实际上,您使用哪种数据访问技术并不重要。并且不要忘记将 using System.Linq; 语句添加到您的控制器文件中,如果它丢失了。

      【讨论】:

      • archil,当我输入_repository.GetAllCategories(). 时,我无法输入Select 而不会引发错误。我使用普通的 ADO.NET 而不是 EF 或 Linq to SQL。这可能是原因吗?如果可能,您能否相应地修改您的答案?
      • @KishorNanda _repository.GetAllCategories() 只是示例代码。一般来说,应该有类型为 IEnumerable 的对象。其实用EF的ADO.NET没关系
      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 2012-12-29
      • 1970-01-01
      相关资源
      最近更新 更多