简体   繁体   中英

Asp.net mvc5:kendo grid ui add a new object instead if editing an object

I'm using the telerik component of kendo grid ui, I'm following demos in the official site but I have an issue. When I create a new object A and then edit that object A, another one is created in the database. The CreateAgency action is invoked not the UpdateAgency . What does it mean? What should I do to fix this issue? Here is my CreateAgency and UpdateAgency actions `

//Create Agency
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateAgency([DataSourceRequest] DataSourceRequest dsRequest, [Bind(Include = "Id,Code,Name,Email,Fax,Tel,TradeRegisterNumber,CompanyId")] AgenceViewModel Agency)
        {
            if (ModelState.IsValid)
            {
                Agency Agencytoadd;
                Agencytoadd = new Agency { Name = Agency.Name, Code = Agency.Code, Email = Agency.Email, Fax = Agency.Fax, Tel = Agency.Tel, TradeRegisterNumber = Agency.TradeRegisterNumber};
                Agencytoadd.Company = db.Companies.Find(Agency.CompanyId);
                db.Agencies.Add(Agencytoadd);
                db.SaveChanges();
                // Get the Id of inserted element
                long id = Agencytoadd.Id;
                // pass the Id of inserted element to CompanyViewModels

            }
            return Json(new[] { Agency }.ToDataSourceResult(dsRequest, ModelState));
        }

        // Update Agency
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateAgency([DataSourceRequest] DataSourceRequest dsRequest, [Bind(Include = "Id,Code,Name,Email,Fax,Tel,TradeRegisterNumber,CompanyId")] AgenceViewModel Agency)
        {
            if (ModelState.IsValid)
            {
                Agency Agencytoupdated = db.Agencies.Find(Agency.Id);

                Agencytoupdated.Code = Agency.Code;
                Agencytoupdated.Name = Agency.Name;
                Agencytoupdated.Email = Agency.Email;
                Agencytoupdated.Fax = Agency.Fax;
                Agencytoupdated.Tel = Agency.Tel;
                Agencytoupdated.TradeRegisterNumber = Agency.TradeRegisterNumber;
                Agencytoupdated.Company = db.Companies.Find(Agency.CompanyId);

                db.Entry(Agencytoupdated).State = EntityState.Modified;
                db.SaveChanges();
            }
            //return Json(ModelState.ToDataSourceResult());
            return Json(new[] { Agency }.ToDataSourceResult(dsRequest, ModelState));
        }
`

As @kul_mi already suggested, your Read should point to action that returns data to show in the grid. You're either missing that action or didn't post it. As for how to properly configure your Kendo UI Grid, you should have something like this

.DataSource(dataSource => dataSource
            .Ajax()
            .Read(r => r.Action("YourActionNameThatReturnsData", "ControllerName")
            .Update(u => u.Action("UpdateAgency", "Agency")
            .Create(c => c.Action("CreateAgency", "Agency")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM