Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How do I print out in "#tb3" the json string from function1.

Right now it prints out:
[object Object]

Index.cshtml
C#
var id1 = parseInt($("#text1").val());
var url1 = "/Home/function1";
var data1 = { "id":id1 };
$.post(url1, data1, function (data) {
    $("#tb3").val(data);

HomeController.cs
XML
[HttpPost]
public JsonResult function1(int id)
{
    List<Table1> the1 = new List<Table1>();
...
    return Json(the1, JsonRequestBehavior.AllowGet);
}
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-15 10:30am    
How static vs instance function can be related to this? Instance function is the one which has "this" passed. And your function 'function1' (never name functions this way!) is the instance function, not static.
What is the problem? What element is the one with id="tb3"? If this is a text box (input element), it should show data.toString().
—SA
teledexterus 4-Mar-15 10:45am    
Here is my code so can you tell me how to print out a json string instead of "[object Object]"

<table>
<tr><td><br /><br /><br /><br /></td></tr>
<tr><td>Input Id:</td><td>@Html.TextBox("text1")</td></tr>
<tr><td colspan="2" align="center"><input id="btn1" type="submit" value="Post Ajax" /></td></tr>
<tr><td colspan="2" align="center"><br /><br /><br />Data</td></tr>
<tr><td> Id:</td><td>@Html.TextBox("tb3")</td></tr>
</table>
Sergey Alexandrovich Kryukov 4-Mar-15 11:21am    
Sure, I answered.
—SA
ZurdoDev 4-Mar-15 10:53am    
data. something. Depends on what you want. Just look at data in the debugger.

1 solution

Of course. You managed to receive already parsed object, parsed from JSON, which is quite an adequate thing to have. Serializing it back to JSON would defeat the purpose of JSON, but you can always do it. (I would assume that you need this for study or debugging purposes.)

Please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900