views:

60

answers:

3

I don't receive an alert even though this successfully returns the model I'm requesting?

function editaddress(id) {
        $.ajax({
            type: "POST",
            url: "/Address/Edit/" + id,
            success: function (msg) {
                alert(msg);
            }
        });
    }

What is msg? I thought it was maybe a JSON object?? When I debug, /Address/Edit/1 returns View(address); but how can I read that object in my view? Do I need to make some other post?

The partial view with this script is a jQuery UI Dialog listing Addresses, and I want to popup another jQuery UI Dialog on top of it to edit the record clicked. So, I need to somehow read the returned model object. How do I do this?

Edit:

public ActionResult Edit(int id)
    {
        Address address = dc.Addresses.Where(x => x.AddressID == id).First();

        return View(address);
    }
+1  A: 

msg is the data returned from the URL "/Address/Edit/" + id in POST format. It doesn't look like you are passing any data to the URL of "/Address/Edit/" + id. If the data is contained in id and you want to include it in the URL, you should use GET. If the URL is '"/Address/Edit.html"and you want to pass itidwith POST, you should includedata:` in your jQuery.

  1. I suggest you initially type out the whole URL just to make your life easier. Include it from http:// to .whatever. Once you get it working like that, you can play around with removing the beginning, but it looks like you might be missing the file type.
  2. You have to format your data and include it with data:

To pass data with post and jQuery you use the format data:"variable1=value1&variable2=value2 ... ", Below it's illustrated using your code.

$.ajax(
{
    type: "POST",
    url: "http://www.yourdomain.com/Address/Edit.html",
    data: "id="+id,
    success: function (msg) 
    {
        alert("Data saved: " + msg);
    }
});

Take a look at the examples on the jQuery.ajax() page:

You have to pass the data in data:, you can't pass the data in the URL if you use POST, you can only do so with GET.

Peter Ajtai
If my action method is indeed returning something, shouldn't I get some kind of alert?
David
@David - Yes. But you would need to include the code at `"/Address/Edit/" + id` for us to understand. It doesn't look like you are passing any data out. Are you sure you want to use POST and not GET?
Peter Ajtai
check out my edit for the action code
David
@David - I update the code. I think your URL may not be correct, and you weren't passing the `id` to the other page from the source.
Peter Ajtai
ASP.NET MVC framework takes care of the routing, so there is no need for a file extension. My action method also handles the URL argument id. My app absolutely expects "/Address/Edit/1" in this case. In Visual Studio debugger, I can observe the break at the return statement, and see that it did in fact return the Address with id=1. But, I still can't read it.
David
A: 

It also helps a lot to use Firebug with Firefox, and use the console. It will show the return AJAX call and the information it contains.

coding_hero
+1  A: 

Use firebug, console. That way yous can see the post you did, see the parameters you passed, and see the response that is returned.

The response will be your "msg" variable

Nealv
thanks for this. i was able to determine the view wasn't rendering correctly simply because i had deleted the Edit view.
David
:) Yes, firebug can be a life saver.These are the steps I take when something goes wrong with an ajax post:- check jquery net to see if everything loads.- check jquery console to see: parameters,post,response.- debug the controller and go into every step to see where it breaks.
Nealv