0

I am using the microsoft ajax and the ajax form looks like this

<% using (Ajax.BeginForm("UserListing", new AjaxOptions
   {
       UpdateTargetId = "results",
       OnComplete = "getData"
   }))
   {%>
<input id="Submit1" type="submit" value="submit" />
<%} %>

Now, i get the getData() js function called upon completion of the ajax request. all i want is that i need to access the response data of this ajax request inside this function and prevent that data being directed to the div with id results.

Thats because i am returning json result from the controller's action method and i need to parse it and display in the div.

script function is :

<script type="text/javascript">
    function getData() {
        alert("Response is : ");
    }
</script>

the div tag is :

 <div id="results">
</div>

I do not want to use other than microsoft ajax. kindly suggest me accordingly.

2 Answers 2

1

You could try rendering the response of the ajax request in a partial view containing only hidden fields. Then in your js function you can access the hidden fields using jQuery selectors.

So your action would look something like

[HttpPost]
public ActionResult UserListing()
{
    List<string> data = GetUserListing();
    return PartialView(data);
}

Your partial view will then only contain hidden fields that you render something like:

<% for (int i = 0; i < Model.Count(); i++)
   { %>
       <input id="<%: "User" + i.ToString() %>" type="hidden" value="<%: Model[i] %>" />
<% } %>

That will render as:

<input id="User0" type="hidden" value="PeterSmith" />

Then in your javaScript function you can access each of the fields by doing something like:

function getData() {
    var user = $('#User0').val();
    alert(user);
}

That will show you the first field rendered. But you can enhance it a bit by looping through all the injected input fields.

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

5 Comments

please note that i was able to get the textual representation of the data via the following code. ` function getData(context) { var ob = new Object(); ob = context.get_data(); alert(ob[0]); }` Now i am looking to parse the text as a Json object. If you have any idea, please tell me.
Hmm..what does the textual representation look like? Are you trying to access an individual item in the data returned from the UserListing action?
You could try something like : 'var myObject = JSON.parse(myJSONtext);'
of course, i have the entire thing in text format and then use the eval() to get the json object then iterate it in for loop like in this code var ob = new Object(); ob = eval(context.get_data()); for (var i = 0; i < ob.length; i++) {....}.. I have difficutly in parsing the date in json format. do you have any json parsers that can work out the right way..
@saranavan: maybe check out this post: link
0

Corrected this problem by myself as explained above. need some json parsers, currently looking for those..

1 Comment

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.