Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo

I design the dhtml form and displaying values from database when updating that display value they are not affecting that particular record .They are saving in new record.

My table columns are Id,Name,Duration,Year,Comments
My view file is
myForm.attachEvent("onButtonClick", function (name, command) {

            if (name == "Update" && command == "Update") {
            
            
                $.ajax({

                    url: '/Home/Update',

                    data: JSON.stringify({
                        
                        Name: myForm.getItemValue("Name"),
                        Duration: myForm.getItemValue("Duration"),
                        Year: myForm.getItemValue("Year"),
                        Comments: myForm.getItemValue("Comments")

                    }),
                    
                    cache: false,
                    dataType: "json",
                    success: function (str) {
                        alert(str.st);
                       

                    },
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8'
                });
               

            }

        });

In my controller
C#
public ActionResult Update(int Id,string Name, int Duration, int Year, string Comments)
        {

            Training d = new Training();

            var st = (from i in dbContext.Trainings
                      where i.Id==Id
                      select i);

            if (d.Id==Id)
            {

                d.Name = Name;
                d.Duration = Duration;
                d.Year = Year;
                d.Comments = Comments;
            }
            try
            {
                this.dbContext.Add(d);
                this.dbContext.SaveChanges();
            }
            catch
            {
                return View();
            }

            var str = new { st = "Successfully updated" };
            return Json(str, JsonRequestBehavior.AllowGet);
        }

If i was updating the operation is not successful if any one knows please help me
Posted
Updated 14-May-12 20:01pm
v3

1 solution

C#
try
           {
               this.dbContext.Add(d);
               this.dbContext.SaveChanges();
           }

This is an ADD code. Not an update code. Thus a new record.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900