but the input parameter id in the action is always emty. How come? I get into the action without a problem but the input parameter is emtpy. Please explain to me why and what a workaround is with $.get.
Even with the $.get the action gets called, that is not the problem. The problem is that the input parameter is empty. With $.post it works but I want it to work with $.get as well. Any solution?
Can you show us your route definitions? In particular, if your route has an
{id} segment, that may be interfering with the ?id=... part of the query string being generated by jQuery. If your route definition assigns a default of
id = "", try changing it to id = UrlParameter.Optional. That basically tells the binder "RouteData might not contain anything useful for
id, so check QueryString instead."
Marked as answer by ricka6 on May 18, 2010 11:19 PM
Nyla Pareska
Member
29 Points
223 Posts
Problem with retrieving input parameter
May 01, 2010 07:27 PM|LINK
I got this actionresult which I want to call with ajax:
public ActionResult TestAction(string id) { ViewData["test"] = id; return View(); }and the call I do is:
$.get('/Test/TestAction', { 'id': 'sfsfsfsf' }, function(data) { $('#feedbackForm').html(data); }, 'html');but the input parameter id in the action is always emty. How come? I get into the action without a problem but the input parameter is emtpy. Please explain to me why and what a workaround is with $.get.
ignatandrei
All-Star
134983 Points
21638 Posts
Moderator
MVP
Re: Problem with retrieving input parameter
May 01, 2010 07:36 PM|LINK
id without apostrophes :
$.get('/Test/TestAction', { id: 'sfsfsfsf' },
Nyla Pareska
Member
29 Points
223 Posts
Re: Problem with retrieving input parameter
May 01, 2010 07:39 PM|LINK
No, that does not work. I have now:
$.get('/Test/TestAction', { id: 'sfsfsfsf' }, function(data) { $('#feedbackForm').html(data); }, 'html');I already tried most things with and without ', ' around the {} itself. Nothing works.
ignatandrei
All-Star
134983 Points
21638 Posts
Moderator
MVP
Re: Problem with retrieving input parameter
May 01, 2010 07:44 PM|LINK
are you using MVC1 or MVC2 RTM?
Nyla Pareska
Member
29 Points
223 Posts
Re: Problem with retrieving input parameter
May 01, 2010 07:58 PM|LINK
MVC2 RTM.
ignatandrei
All-Star
134983 Points
21638 Posts
Moderator
MVP
Re: Problem with retrieving input parameter
May 01, 2010 08:38 PM|LINK
Just to make sure :
1. Modify from "get" to "post"
2. Modify from '/Test/TestAction' to '<% = ResolveUrl("/Test/TestAction") %>'
3. let id, not 'id'
Now put a breakpoint on
return View();
and see if it is running into...
Nyla Pareska
Member
29 Points
223 Posts
Re: Problem with retrieving input parameter
May 01, 2010 08:44 PM|LINK
Even with the $.get the action gets called, that is not the problem. The problem is that the input parameter is empty. With $.post it works but I want it to work with $.get as well. Any solution?
ignatandrei
All-Star
134983 Points
21638 Posts
Moderator
MVP
Re: Problem with retrieving input parameter
May 01, 2010 08:50 PM|LINK
Read Asp.NET MVC 2 documentation . There is a breaking change from 1 to 2 - in order to accept get for return Json, you must specify
Nyla Pareska
Member
29 Points
223 Posts
Re: Problem with retrieving input parameter
May 01, 2010 08:53 PM|LINK
But I am returning html, not json. And the html gets returned as expected. It is just the input parameter which does not arrive so it is empty.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Problem with retrieving input parameter
May 02, 2010 09:30 PM|LINK
Can you show us your route definitions? In particular, if your route has an {id} segment, that may be interfering with the ?id=... part of the query string being generated by jQuery. If your route definition assigns a default of id = "", try changing it to id = UrlParameter.Optional. That basically tells the binder "RouteData might not contain anything useful for id, so check QueryString instead."