php待办事项设计,如何为待办事项列表应用程序设计ViewModel?

本文介绍了如何在ASP.NET MVC应用中创建并填充下拉列表。通过定义`TaskModel`和`Category`类,以及`GetCategories`方法,实现了从模型加载类别数据到视图,并在`CreateTask`操作中处理用户输入。文章着重展示了如何在创建任务时利用数据绑定来实现类别选择的交互功能。
摘要由CSDN通过智能技术生成

基肖尔,

最好的选择是拥有为您的任务和类别定义的模型(一体化)

这里是一切如何挂在一起。

哪里

IEnumerable Categories用于创建可供使用的下拉列表

model.NewTask.categoryId, Model.Categories) %>这将创建一个很好的下拉列表

private IEnumerable GetCategories

{

get

{

List categories = new List

{

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);

}

}

///

/// your model for creation

///

public class TaskModel

{

public Task NewTask { get; set; }

public IEnumerable Categories { get; set; }

}

///

/// Task

///

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; }

}

///

/// Category

///

public class Category

{

public int categoryId { get; set; }

public string categoryName { get; set; }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值