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.

Saravanan
  • 7,637
  • 5
  • 41
  • 72

2 Answers2

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.

tobias86
  • 4,979
  • 1
  • 21
  • 30
  • 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. – Saravanan Mar 24 '11 at 08:27
  • Hmm..what does the textual representation look like? Are you trying to access an individual item in the data returned from the UserListing action? – tobias86 Mar 24 '11 at 10:16
  • You could try something like : 'var myObject = JSON.parse(myJSONtext);' – tobias86 Mar 24 '11 at 10:17
  • 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.. – Saravanan Mar 24 '11 at 10:57
  • @saranavan: maybe check out this post: [link](http://stackoverflow.com/questions/1792865/how-to-use-json-parse-reviver-parameter-to-parse-date-string) – tobias86 Mar 24 '11 at 12:11
0

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

Saravanan
  • 7,637
  • 5
  • 41
  • 72