locked
Passing value from JQuery to Razor Web Pages RRS feed

  • Question

  • User-701766627 posted

    I'm trying to pass a value from JQuery to Razor Web Pages. It seems like I'm missing something in the syntax.

    I have the following:

    var CurrentYear = new Date().getFullYear();
    
    $.post('/Timesheet/Login/GeneratePDF.cshtml', {StartDate:CurrentYear}, "json");
    
    window.location.replace("/Timesheet/Login/GeneratePDF.cshtml");

    This is how I'm reading the value in my file Generate PDF.cshtml:

    var StartDate_ = Request["StartDate"];
    

    I could use a different method, but this is all I'm familiar with. I just want something simple.

    This is where I got the idea from https://www.mikesdotnetting.com/article/202/inline-editing-with-the-webgrid

    Thanks in advance.

    Thursday, March 2, 2017 8:59 PM

All replies

  • User-2057865890 posted

    Hi Oscar Duran,

    Syntax

    $(selector).post(URL,data,function(data,status,xhr),dataType)

    The $.post() method loads data from the server using a HTTP POST request which is equivalent to:

    $.ajax({
      type: "POST",
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });

    reference:

    https://api.jquery.com/jquery.post/

    https://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages

    Best Regards,

    Chris

    Friday, March 3, 2017 1:43 AM
  • User-701766627 posted

    Thank you very much Chris. I tried this and it didn't really work:

    $(selector).post('/Timesheet/Login/GeneratePDF.cshtml', {StartDate:CurrentYear}, "json");

    All I'm missing there is:

    function(data,status,xhr)

    But I believe that is optional. if it isn't optional, I'm not sure what to enter for function.

    Friday, March 3, 2017 3:16 PM
  • User-2057865890 posted

    Hi Oscar,

    Try

    $.post("/Timesheet/Login/GeneratePDF.cshtml", {StartDate:CurrentYear} );

    or

    $.post("/Timesheet/Login/GeneratePDF.cshtml", {StartDate:CurrentYear}, function(result){
         alert("success")
     });

    reference: https://www.w3schools.com/jquery/ajax_post.asp

    Best Regards,

    Chris

    Tuesday, March 14, 2017 7:24 AM