English 中文(简体)
• 如何设计用于清单应用的视角模型?
原标题:How to design a ViewModel for a todo list application?

我正在制作一个简单的语气应用,有两个实体:taskscategories

为创建<条码>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; }
}

本类实体

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

我如何设计<代码> 缩略语 因此,我可以对<代码><> 类别/代码>在<代码>上的约束?

<><>Edit>: 我正在使用传统的《反歧视法》。 NET而不是实体框架或LINQ至Q。

问题回答

Kishor,

最好有模式,为你的任务和类别(均为一类)下定义。

这里,所有东西如何一起hang。

地点

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

在《任务评论》中(我更喜欢点名创建TaskViewModel)为选定名单的类别设定了财产。

public IEnumerable<SelectListItem> CategoriesSelectList;

在控制器中,在归还观点之前对该财产进行约束(指出在示范国无效时,也应在邮递中这样做)。

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)

<><>Edit>:

In your code, _repository.AllCategories() should be replaced by your data access code, that returns object having type IEnumerable<Category>. It actually does not matter which data access technology you use. And do not forget to add the using System.Linq; statement to your controller file, if it s missing.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签