MVC批量编辑 - Sql的Linq列表保存 [英] MVC Bulk Edit - Linq to Sql List Save

查看:231
本文介绍了MVC批量编辑 - Sql的Linq列表保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要得到我的头一轮使用MVC和LINQ一些基本面SQL我工作的适应斯蒂芬·瓦尔特的的任务列表应用

To get my head round some fundamentals using MVC and Linq to SQL I'm working on an adaption to Stephen Walther's TaskList application:

我添加使用<一个描述的概念批量编辑系统href=\"http://blog.$c$cville.net/2008/12/22/editing-a-variable-length-list-of-items-in-aspnet-mvc/\">Steve桑德森的博客。

这所有的作品如预期,但是,我无法救了我返回的任务列表。在后到我BulkEdit遍历返回列表和任务的我LinqToSql数据库列表更新每一个项目。

This all works as expected, however, I'm having trouble saving my returned List of Tasks. The Post to my BulkEdit loops through the returned list and updates each item in my LinqToSql db list of Tasks.

我BulkEdit视图继承的ViewPage&LT;名单,LT; TaskList.Models.Task&GT;&GT; 键,如下:

My BulkEdit view inherits ViewPage<List<TaskList.Models.Task>> and is as follows:

<% 
using (Html.BeginForm())
{
%>
        <div id="items">

<%
            foreach (var task in ViewData.Model)
            {
                Html.RenderPartial(
                    "TaskEditor",
                    task,
                    new ViewDataDictionary(ViewData)
                            {
                                {"prefix", "tasks"}
                            }
                );
            }
%>

        </div>

        <input type="submit" value="Save changes" />

<%
    }
%>

该TaskEditor控件继承 System.Web.Mvc.ViewUserControl&LT; Models.Task&GT; ,看起来像这样:

<div>
<%= Html.Hidden(ViewData["prefix"] + ".index", ViewData.Model.Id) %>

<% var fieldPrefix = string.Format("{0}[{1}].", ViewData["prefix"], ViewData.Model.Id); %>

<%= Html.Hidden(fieldPrefix + "Id", ViewData.Model.Id) %>
Description:
<%= Html.TextBox(fieldPrefix + "TaskDescription", ViewData.Model.TaskDescription)%>
Date:
<%= Html.TextBox(fieldPrefix + "EntryDate", ViewData.Model.EntryDate.ToString("o"))%>   
Completed:
<%= Html.CheckBox(fieldPrefix + "IsCompleted", ViewData.Model.IsCompleted)%>
</div>

控制器GET和POST方法如下:

The Controller Get and Post methods are as follows:

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult BulkEdit()
    {
        var tasks = from t in db.Tasks orderby t.EntryDate descending select t;

        return View(tasks.ToList());
    }        

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult BulkEdit(IList<Task> tasks)
    {
        foreach(Task task in tasks)
        {
            foreach(Task dbTask in db.Tasks)
            {
                if (dbTask.Id == task.Id)
                {
                    dbTask.TaskDescription = task.TaskDescription;
                    dbTask.EntryDate = task.EntryDate;
                    dbTask.IsCompleted = task.IsCompleted;
                }
            }
        }

        db.SubmitChanges();

        return RedirectToAction("Index");
    }

我的问题是,这似乎太复杂了,我还没有占到被添加或从列表中删除任务。我想preFER做的是一样的东西。

My question is, this seems too complicated and I haven't yet accounted for tasks being added or deleted from the list. What I would prefer to do is something like

db.Tasks = tasks;

和LINQ的让做这一切的神奇制定出哪些已经改变,哪些是新/老了。

and let Linq do all its magic to work out which ones have changed and which ones are new / old.

这可能吗?还是我期待有点从LINQ的太多了这么快?

Is this possible? Or am I expecting a bit too much from Linq so soon?

推荐答案

您正在处理的事实,LINQ to SQL的不具有多层的故事。我认为这是你在找什么:

You're dealing with the fact that LINQ to SQL has no multi-tier story. I think that this is what you're looking for:

http://blog.irm.se/blogs/eric/archive/2008/08/20/Go-Distributed-With-LINQ-to-SQL.aspx

合并方法,这家伙presents可以很容易地变成DataContext类的扩展方法,它几乎是就像它被内置到的LINQ to SQL。我说差不多,因为你必须包括在您的扩展方法位于为了使用它的命名空间。

The Merge method that this guy presents could be easily turned into an "extension method" of the DataContext class and it would almost be just like it was built right into LINQ to SQL. I say almost because you'd have to include the namespace of where your extension method is located in order to use it.

这篇关于MVC批量编辑 - Sql的Linq列表保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆