How to retrieve posted json or normal object or text in MVC?http://forums.asp.net/t/1797834.aspx/1?How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Fri, 27 Apr 2012 21:03:11 -040017978344955411http://forums.asp.net/p/1797834/4955411.aspx/1?How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+How to retrieve posted json or normal object or text in MVC? <p>Hi,</p> <p>i've a function in javascript that sends ajax HTTPPost &nbsp;request to server. it sends an object to server.</p> <p>How can i retrieve that object in controller in MVC? ( model minding is not working here);&nbsp;</p> 2012-04-27T19:06:17-04:004955473http://forums.asp.net/p/1797834/4955473.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>Which version of MVC are you using? MVC3 introduced the ability to model bind json.</p> <p>What does your ajax request look like? Also what does the model and controller code look like? Can you share those on here? It will be easier to diagnose the problem if we can see the code. Thanks.</p> 2012-04-27T19:51:26-04:004955490http://forums.asp.net/p/1797834/4955490.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>HI,</p> <p>My ajax request updates the database, it doesn't take care of response. And the ajax code is here: it is more like posting a comment on &nbsp;a post but doesn't need any response.&nbsp;</p> <pre class="prettyprint">document.getElementById('addbtn').addEventListener(&quot;click&quot;, function add1() { var text = { name: document.getElementById('addtext').value }; var r = new XMLHttpRequest(); r.open('POST', '/update/add'); r.setRequestHeader(&quot;Content-Type&quot;, &quot;text/plain;charset=UTF-8&quot;); r.send(text); }, false);</pre> <p>and Controller is this:</p> <pre class="prettyprint">[HttpPost] public ActionResult add(menu menu) { db.menus.Add(menu); db.SaveChanges(); return null; }</pre> <p>I'm sure controller code is wrong. the thing is if i can know how to acces the posted object "text" in c# code in controller, then i will update my database.&nbsp;</p> <p>for information model looks like this:</p> <pre class="prettyprint">public class menu { public int id { get; set; } public string name { get; set; } public ICollection&lt;navmenu&gt; navmenu { get; set; } public DateTime date { get; set; } }</pre> <p><br> <br> </p> <p></p> <p><br> <br> </p> 2012-04-27T20:06:58-04:004955500http://forums.asp.net/p/1797834/4955500.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>I'm a little confused, so it does update the database, but it's not redirecting you?</p> <p>If that's the case, then the problem is that your controller is performing a redirect. That will not redirect the page, but attempt to send a redirect back to the ajax request. The ajax request is a completely different thing from the page itself, so redirection there will not move the page.</p> <p>Instead you want need to do is send back a code or something and have the javascript redirect the page using location.href=&quot;target url&quot;;</p> <p></p> 2012-04-27T20:23:21-04:004955505http://forums.asp.net/p/1797834/4955505.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>Controller's action returns &quot; null&quot;;&nbsp;</p> <p>so, after the database is updated, connection with client will be closed as nothing to send to client. even if something it sends, client's code won't recieve anything. and wont update its DOM as it is not expecting any kind of response from server.&nbsp;</p> <p>So the idea is, user posts a comment on a post, then that string is sent to server in an object it could be json or javascript . in the above code, comment is obtained from a text box, and is sent to server with code &quot;r.send(text)&quot;. text is an object with a property called &quot;name&quot;.&nbsp;</p> <p>and javascript on the browser appends that comment to the post after it posts the comment to server. server takes that object and updates database, and return 'null' means &quot;not returning any repsonse&quot;.&nbsp;</p> <p>:D</p> 2012-04-27T20:34:09-04:004955523http://forums.asp.net/p/1797834/4955523.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>Ok part of the problem is that you need to speciy the data type as json and the content type as application/json. You are setting it to text/plain.</p> <p>Try changing the content type to:</p> <pre class="prettyprint">r.setRequestHeader(&quot;Content-Type&quot;, &quot;application/json;charset=UTF-8&quot;);</pre> <p><br />&nbsp;</p> <p>Have you considered using jquery? This would make your ajax call much simpler . For example:</p> <pre class="prettyprint">&lt;script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; //on load handler &#36;(function(){ &#36;("#addbtn").click(function(){ var text = { name: &#36;("#addtext").val() }; &#36;.ajax({ type: "POST", url: "/update/add", dataType: 'json', contentType: 'application/json; charset=utf-8', data: text }); }); }); &lt;/script&gt;</pre> <p><br />&nbsp;</p> <p>In your action method you need to return json, not a redirect.</p> <pre class="prettyprint">[HttpPost] public ActionResult add(menu menu) { db.menus.Add(menu); db.SaveChanges(); //this sets the correct headers so that the javascript knows the connection is complete return Json(new { status = "saved"; }); }</pre> <p><br> <br> </p> 2012-04-27T20:47:59-04:004955528http://forums.asp.net/p/1797834/4955528.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>:D</p> <p>okay, thanks for you reply:</p> <p>it seems you didn't understand the problem.</p> <p>my problem is, i'm unable to store the data in database. the &quot;menu&quot; object i'm recieving is not being bound to the database model.&nbsp;</p> <p>it is being empty when i debug the application.&nbsp;</p> <p>so the problem is &quot; i'm unable to retrieve the object i am sending in the HTTP Header. i dont &nbsp;know how to retieve the object using Request object. &quot;</p> <p>usually, when we are submiting the forms,&nbsp;</p> <p>Request.Form[&quot;key&quot;];</p> <p>will be used to retieve the information of a property of the object sent in post request.&nbsp;</p> <p>but here in my code, i'm not using forms. so, i'm just sending a normal javascript object. i can modify the application and send json object to server. so, how can i access manually acces the object (javascript /json) as model biding is not working here?</p> <p></p> <p></p> 2012-04-27T20:56:54-04:004955535http://forums.asp.net/p/1797834/4955535.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>Now it's working. i checked out this post:&nbsp;</p> <p><a href="http://stackoverflow.com/questions/6287579/how-do-i-get-posted-data-in-mvc-action">http://stackoverflow.com/questions/6287579/how-do-i-get-posted-data-in-mvc-action</a></p> <p>solved my problem!</p> <p>thanks for you help @CodeHobo</p> 2012-04-27T21:02:07-04:004955538http://forums.asp.net/p/1797834/4955538.aspx/1?Re+How+to+retrieve+posted+json+or+normal+object+or+text+in+MVC+Re: How to retrieve posted json or normal object or text in MVC? <p>Yes, I understand that your model isn't binding. That's why my first suggestion was to set the content type since without the correct content type, the json model binder won't be invoked. You need to set the content type of the request to&nbsp;'application/json; charset=utf-8'</p> <p>Another option is not to model bind and just extract name manually. Try this:</p> <pre class="prettyprint">[HttpPost] public ActionResult add(string name) { Menu menu = new Menu(); menu.name = name; db.menus.Add(menu); db.SaveChanges(); return null; }</pre> <p>Also do keep in mind that in the action method you can still access Request.Form directly if you need to.<br> &nbsp;</p> 2012-04-27T21:03:11-04:00