2

i have a simple form on my page with a textbox and button that calls this javascript method

onclick="Create();"

and this is the function that is ran...

function Create() {
    var txt = document.getElementById("longTxt").value;

    if (txt == "Insert Name") {
        alert("You must provide a name");
        return;
    }

    var data = { Name: txt };
    $.post("/Home/Create", data, null, "json");
}

The action called inserts the name into the database and returns a string.

The method works it adds the posted name to my database but how do i use that string my method returns to display the returned string in my textbox.

Edit:

what is my method suppose to look like i have something like

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Create(string name)
    {
        string hash = repo.addName(name);
        return new JsonResult()
        {
            Data = new { data = hash }
        };

    }

when i add breakpoints and check it out it looks like it is working it does add the name. the callback function is used because i replaced the

$("#longUrl").val(json.PropertyReturned);

with an alert(json.PropertyReturned); to see if anything is even happening and i get the textbox but it says "undefined"

im really new to all of this maybe im just doing the method and return wrong. also intelisense does not show "json.PropertyReturned" as an option when it pops up after typing "json."

1
  • +1. Thanks Fatal. This question and subsequent answers have helped on a few different issues/questions that I've had. Much appreciated! Commented Oct 6, 2009 at 16:55

4 Answers 4

5

Provide a callback as the third argument of the post call that will get the value returned from the method and put it in your textbox.

$.post("/Home/Create", data, function(json,textStatus) {
    $("#longTxt").val( json.data );
},"json");

If you need to handle errors as well, you might want to look at using $.ajax().

EDIT: You should probably use the controller's Json method instead of constructing the result using the default constructor. It's more convenient.

return Json( new { data = hash } );
Sign up to request clarification or add additional context in comments.

3 Comments

@tvanfosson, thank you for your reply. I still have a slight issue and I've updated my post to reflect it. Could you take a look.
@Fatal510 -- I should have pointed out that the name of the property I was using was fictious since I didn't know your actual structure. I've updated. Note I also give an alternative way to return the json result using the controller's Json method.
@tvanfosson, Thanks for the additional information it really helped.
0

I think the third parameter is the callback function. Instead of a null, pass a function name there.

Comments

0
$.post("/Home/Create", data, function(data) {
    $("#textboxid").val(data.PropertyName);}, "json")

Comments

0

Provide a callback, or set the async to false.

Comments

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.