Click here to Skip to main content
14,427,455 members
Rate this:
Please Sign up or sign in to vote.
JS grabbing text from text-boxes, passing to asp.net mvc ActionResult but ActionResult parameters appear null

I have 2 textboxes which are filled by 2 datepickers upon the user's selection but although the information grabs correctly from the textboxes when I use the below javascript to pass it to the actionresult. The parameters of the actionresult show to be null.

How do I fix?


Code:

@Html.Label("Start", "Start Date:")
   @Html.TextBox("Start", string.Empty, new {@id = "Start", @class = "datepicker"})
   @Html.Label("endd", "End Date:")
   @Html.TextBox("endd", string.Empty, new {@id = "End", @class = "datepicker"})
   <input type="submit" value="Apply" id ="DateSelected" />


   <script type="text/javascript">
     $('.datepicker').datepicker();
   $("#DateSelected").click(function () {
           var startD = $('#Start').val().toString();
           var endD = $('#End').val().toString();
                   $.ajax({
                   url: '/Docs/FirstDoc',
                   type: "Post",
                   data: { start: String.valueOf(startD), end: String.valueOf(endD) },
               });
   </script>



Action Result:

[HttpPost]
        public ActionResult FirstDoc(string start, string end)
        {
            FirstDocModel firstDocModel = FirstDocHelper.RunFunction(start, end);
            return PartialView(firstDocModel);
        }
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The valueOf[^] method doesn't take any parameters, so you're effectively trying to pass the native value of the String type.

Remove the String.valueOf call from the parameters:
data: { start: startD, end: endD }
   
Comments
Empir 23-Oct-14 10:23am
   
That makes no difference, I placed the String.valueOf as I had no idea what the issue was, so thought it couldn't hurt
Richard Deeming 23-Oct-14 10:26am
   
The String.valueOf() call does make a difference - it returns a value which cannot be serialized with JSON, which causes the AJAX call to send an empty object.

If you're still not getting the parameter values after removing those calls, then you need to debug your code to check that the startD and endD variables contain the values you expect.
Empir 23-Oct-14 10:28am
   
I meant, even when I remove String.valueOf(), the same issue is still present
Richard Deeming 23-Oct-14 10:29am
   
So debug your code. Make sure that the startD and endD variables contain the values you expect.
Empir 23-Oct-14 10:39am
   
They do, they each contain the dates, they should

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100