views:

64

answers:

2

I've got a web page where the user can add a new item to a category and that item is saved to the database. However, the ID of the item is an identity value for the database table, so I don't know what the ID will be, and I need this ID so that the user can edit the item later.

I'm using ajax to make a POST submit:

$.ajax({
    url: 'TumourGroup/CreateSub',
    type: "POST",
    data: {'pos': index, 'text': text }
});

This is linked to an MVC model with three fields, ID, pos, and text. Is there a way for me to get the ID that's generated on POST submit by having CreateSub(int index, string text) return something?

+1  A: 

Have CreateSub(int index, string text) return the type that the ID column is (Int32 maybe?), jQuery can then use this value like this:

$.ajax({
    url: 'TumourGroup/CreateSub',
    type: "POST",
    data: {'pos': index, 'text': text },
    success: function(data) {
      alert("Newly created ID: " + data); //data will be the ID
    }
});
Nick Craver
That works, thank you!
sslepian
A: 

Just do as Nick Craver suggested, but generate the answer in JSON and consume it with getJSON http://api.jquery.com/jQuery.getJSON/

VP
JSON is great for most things...but when you need to return a single integer, it's over-complication.
Nick Craver