0

I'm new to updating Enity Framework objects using JQuery and JSON. I created the entity objet in a console app and the update function and it works. I tried tranfering over to my web app and I can't get it to update through my web app.

Controller Code

[HttpPost]
    public ActionResult Update(int rowID)
    {
        var keyValues = new KeyValuePair<string, object>[] {
        new KeyValuePair<string, object>("QUICKFIX_ID", rowID)
        };

        var key = new EntityKey("CSCEntities.tbl_Quickfix_Toolbar", keyValues);

        var quickfixtbl = (tbl_Quickfix_Toolbar)db.GetObjectByKey(key);
        int usrcount = (int)quickfixtbl.USECOUNT_NR;

        // update
        usrcount = usrcount + 1;

        // find the row to update using the ID
        tbl_Quickfix_Toolbar quickFix = (from x in db.tbl_Quickfix_Toolbar
                                         where x.QUICKFIX_ID == rowID
                                         select x).First();
        quickFix.USECOUNT_NR = usrcount;
        db.SaveChanges();

        return View();
    }

and here is the JQueryfunction that calls my controller:

//Updates the use count in the List


function Update() {
  $.ajax({
          url: "/csc/devapp1/Home/Update",
          type: 'POST',
          data: "rowID=" +id,
          contentType: "application/json;charset=utf-8",
          success: function (data) {
                   alert('Details Updated Successfully');
          },
          error: function () {
                   alert('Unable to Update for the Given ID');
          }
   });

Please let me know what I'm doing wrong, I been going at this for weeks and can't get it to work.

1 Answer 1

2

change this line:

data: "rowID=" +id,

to this:

data: JSON.stringify({ rowID : id }),

And probably your URL is wrong:

change this:

url: "/csc/devapp1/Hom/Update/",

to this:

url: '/Home/Update',

And it should work

Sign up to request clarification or add additional context in comments.

3 Comments

You could send in a value to your js for url also. So Url would be something like var [email protected]("Update","controller",new {[email protected]}); js.doSomething({url=url});
I updated the questions URL. I noticed I misspelled the URL *Home controller. I'm using JQGrid and when I tried using @Url.Action("Update","Home") it broke my code. The JQGrid doesn't display anymore.
It should but I'm not getting any updates to the DB using the Entity Framework. This is why I'm trying to figure out what I'm doing wrong and how I can go about a different way to get this to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.