Asp.net mvc 3 MVC3 Telerik批量编辑更新更新客户端,但不更新数据库表

Asp.net mvc 3 MVC3 Telerik批量编辑更新更新客户端,但不更新数据库表,asp.net-mvc-3,batch-file,grid,telerik,Asp.net Mvc 3,Batch File,Grid,Telerik,我在Telerik Extensions for ASP.NET MVC中的编辑批处理示例中实现了一个MVC3/Razor网格控件,在大多数情况下,我认为它可以工作,但是我注意到,虽然更新似乎在客户端上工作,但底层数据库表没有更新 我认为我可能在实现这一点时错过了一些步骤,但是在使用Northwind数据库下载的Telerik示例中的产品名称的批量编辑示例中进行更新后,基础数据表也没有更改 视图: 存储库: namespace MarketingWebsiteTools.Models {

我在Telerik Extensions for ASP.NET MVC中的编辑批处理示例中实现了一个MVC3/Razor网格控件,在大多数情况下,我认为它可以工作,但是我注意到,虽然更新似乎在客户端上工作,但底层数据库表没有更新

我认为我可能在实现这一点时错过了一些步骤,但是在使用Northwind数据库下载的Telerik示例中的产品名称的批量编辑示例中进行更新后,基础数据表也没有更改

视图:

存储库:

namespace MarketingWebsiteTools.Models
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using MarketingWebsiteTools.ViewModels;
    using MarketingWebsiteTools.Models;

    public static class SessionCalloutRepository
    {
        public static IList<EditableCallout> All()
        {
            WebsiteDataContext wdc = new WebsiteDataContext();
            //CalloutContext db = new CalloutContext();
            IList<EditableCallout> result = (IList<EditableCallout>)HttpContext.Current.Session["Callouts"];

            if (result == null)
            {
                HttpContext.Current.Session["Callouts"] = result =
                   (from c in wdc.CalloutToProducts
                    join cv in wdc.CalloutValues on c.CalloutID equals cv.CalloutID
                    select new EditableCallout
                    {
                        id = c.id,
                        ProductIdentifier = c.ProductIdentifier,
                        DateStart = c.DateStart,
                        DateEnd = c.DateEnd,
                        Value = cv.Value,
                        IsActive = c.IsActive
                    }).ToList();
            }

            return result;
        }


        public static EditableCallout One(Func<EditableCallout, bool> predicate)
        {
            return All().Where(predicate).FirstOrDefault();
        }
        public static void Insert(EditableCallout callout)
        {
            callout.id = All().OrderByDescending(c => c.id).First().id+ 1;
            All().Insert(0, callout);
        }

        public static void Update(EditableCallout callout)
        {
            EditableCallout target = One(c => c.id == callout.id);
            if (target != null)
            {
                target.ProductIdentifier = callout.ProductIdentifier;
                target.DateEnd = callout.DateEnd;
                target.DateStart = callout.DateStart;
                target.Value = callout.Value;
                target.IsActive = callout.IsActive;

            }
        }
        public static void Delete(EditableCallout callout)
        {
            EditableCallout target = One(c => c.id== callout.id);
            if (target != null)
            {
                All().Remove(target);
            }
        }
    }
}
谢谢


道格

Telerik演示故意不更新基础数据库。所有更改都保存在会话的内存中。您需要添加更新数据库所需的代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using Telerik.Web.Mvc;
using MarketingWebsiteTools.Models;
using MarketingWebsiteTools.ViewModels;
//using MarketingWebsiteTools.Services;
using MarketingWebsiteTools.Filters;

namespace MarketingWebsiteTools.Controllers
{ 
    public partial class CalloutsController : Controller
    {
        [SourceCodeFile("EditableCallout", "~/Models/EditableCallout.cs", Order=1 )]
        [SourceCodeFile("SessionCalloutRepository", "~/Models/SessionCalloutRepository.cs", Order = 2)]
        [SourceCodeFile("SessionCalloutRepository", "~/Models/SessionCalloutRepository.cs", Order = 3)]

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(SessionCalloutRepository.All()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [CultureAwareAction]
        [GridAction]
        public ActionResult _SaveBatchEditing([Bind(Prefix =
            "inserted")]IEnumerable<EditableCallout> insertedCallouts,
            [Bind(Prefix = "updated")]IEnumerable<EditableCallout> updatedCallouts,
            [Bind(Prefix = "deleted")]IEnumerable<EditableCallout> deletedCallouts)
        {
            if (insertedCallouts != null)
            {
                foreach (var callouts in insertedCallouts)
                {
                    SessionCalloutRepository.Insert(callouts);
                }
            }
            if (updatedCallouts != null)
            {
                foreach (var callouts in updatedCallouts)
                {

                    var target = SessionCalloutRepository.One(p => p.id == callouts.id);
                    if (target != null)
                    {
                        target.DateStart = callouts.DateStart;
                        target.DateEnd = callouts.DateEnd;
                        target.Value = callouts.Value;
                        target.IsActive = callouts.IsActive;
                        SessionCalloutRepository.Update(target);
                    }
                }
            }
            if (deletedCallouts != null)
            {
                foreach (var product in deletedCallouts)
                {
                    SessionCalloutRepository.Delete(product);
                }
            }
            return View(new GridModel(SessionCalloutRepository.All()));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace MarketingWebsiteTools.Models
{
    //[KnownType(typeof(EditableCallout))]
    public class EditableCallout
    {
        //[ScaffoldColumn(false)]
        [DisplayName("id")]
        public int id { get; set; }
        [Required]
        [DisplayName("ProductIdentifier")]
        public string ProductIdentifier { get; set; }

        [DisplayName("DateStart")]
        public DateTime? DateStart { get; set; }

        [DisplayName("DateEnd")]
        public DateTime? DateEnd { get; set; }

        [DisplayName("IsActive")]
        public int? IsActive { get; set; }

        [DisplayName("Value")]
        public string Value { get; set; }
    }
}
namespace MarketingWebsiteTools.Models
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using MarketingWebsiteTools.ViewModels;
    using MarketingWebsiteTools.Models;

    public static class SessionCalloutRepository
    {
        public static IList<EditableCallout> All()
        {
            WebsiteDataContext wdc = new WebsiteDataContext();
            //CalloutContext db = new CalloutContext();
            IList<EditableCallout> result = (IList<EditableCallout>)HttpContext.Current.Session["Callouts"];

            if (result == null)
            {
                HttpContext.Current.Session["Callouts"] = result =
                   (from c in wdc.CalloutToProducts
                    join cv in wdc.CalloutValues on c.CalloutID equals cv.CalloutID
                    select new EditableCallout
                    {
                        id = c.id,
                        ProductIdentifier = c.ProductIdentifier,
                        DateStart = c.DateStart,
                        DateEnd = c.DateEnd,
                        Value = cv.Value,
                        IsActive = c.IsActive
                    }).ToList();
            }

            return result;
        }


        public static EditableCallout One(Func<EditableCallout, bool> predicate)
        {
            return All().Where(predicate).FirstOrDefault();
        }
        public static void Insert(EditableCallout callout)
        {
            callout.id = All().OrderByDescending(c => c.id).First().id+ 1;
            All().Insert(0, callout);
        }

        public static void Update(EditableCallout callout)
        {
            EditableCallout target = One(c => c.id == callout.id);
            if (target != null)
            {
                target.ProductIdentifier = callout.ProductIdentifier;
                target.DateEnd = callout.DateEnd;
                target.DateStart = callout.DateStart;
                target.Value = callout.Value;
                target.IsActive = callout.IsActive;

            }
        }
        public static void Delete(EditableCallout callout)
        {
            EditableCallout target = One(c => c.id== callout.id);
            if (target != null)
            {
                All().Remove(target);
            }
        }
    }
}