locked
Unable to send string to a controller method via ajax RRS feed

  • Question

  • User-259252065 posted

    I'm implementing asp.net core 3.1. I want to send a "year" value as the input parameter of my method controller. when I debug my project I can see that "year" has a value in ajax call but the argument of year in the controller method is null. I appreciate if anyone can suggest me to solve the issue. Here is the related code in razor view:

     $('#exampleModal').modal('@ViewBag.ModalState');
    
                $('#exampleModal').on('show.bs.modal', function (event) {
                    var button = $(event.relatedTarget);
                 
                    var year = $("input[name='Year']:checked").val();
                    console.log('myyear:' + year);
      //My problem is here, when sending year value to the 
      //ProductDetails method         
                    $.get('@Url.Action("ProductDetails", "Home")/' + {year: year}, function (data) {
    
    
                    $.each(data, function (index, value) {
    
    
    
                        var markup = "<tr><td>" + value.apName +"</td></tr>";
    
                        $("#classTable").append(markup);
                   })
                });
            });

    Home controller method:

    public IList<ApiApplicantDTO> ProductDetails(string year)
            {
                
                Console.WriteLine("year:" + year);
    }

    Saturday, October 31, 2020 12:03 PM

All replies

  • User475983607 posted

    The URL is incorrect...

    var year = $("input[name='Year']:checked").val();
    var url = '@Url.Action("ProductDetails", "Home")?year=' + year;
    $.get(url, function (data) {

    Saturday, October 31, 2020 1:33 PM
  • User-474980206 posted

    You pass the data as a parameter 

          $.get('@Url.Action("ProductDetails", "Home")', {year: year}, function (data) {
    
    Saturday, October 31, 2020 3:42 PM