Results 1 to 1 of 1

Thread: [RESOLVED] jscript ajax post from view to controller

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Resolved [RESOLVED] jscript ajax post from view to controller

    Hey guys I am new to MVC, javascript, ajax, and C#. Fresh over from VB .Net. I am working with MVC 5 at the moment. I have a jQuery datepicker in a View and onSelect I have it doing a function in jQuery that gets the currentDate and uses ajax to send the post to the controller. The jscript gets the current date ( although my format doesn't work currently ), and sends the post, but the post is either sending null data, or the controller is not accepting it.

    javascript Code:
    1. <script type="text/javascript">
    2.    
    3.     $(function () {
    4.                 $('#datepicker').datepick({ onSelect: getDate, dateFormat: "yy-mm-dd" });
    5.         function getDate(date)
    6.         {
    7.             var currentDate = $("#datepicker").datepick("getDate");
    8.        
    9.         $.ajax({
    10.             type: "post",
    11.             url: "SelectDay",
    12.             dataType: "json",
    13.             data: { theDate: currentDate }
    14.            })
    15.         }
    16.     });
    17.         </script>

    c# Code:
    1. // GET: /Timeclock/SelectDay
    2.         public ActionResult SelectDay(string theDate)
    3.         {
    4.            TimeclockViewModel tt = new TimeclockViewModel();
    5.             // stuff goes here                  
    6.            return View(tt);
    7.         }

    lets get this MVC subforum going



    Solved:

    Always remember to fully put your /Ctrl/Actn

    Leaving "SelectDay" would link to the right action in the controller, but not actually send the data to it.

    I had to put "/Timeclock/SelectDay" to actually recieve the data.

    Code:
    $.ajax({
                type: 'POST',
                url: '/Timeclock/SelectDay',
                traditional: true,
                dataType: "json",
                data: { theDate: currentDate }
                })
    Last edited by thebuffalo; Apr 2nd, 2014 at 12:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width