Home > front end >  Passing Parameter into MVC Controller
Passing Parameter into MVC Controller

Time:12-27

I'm on a project right now and have a problem. How to properly pass the expenseId to the controller so I can take the value from the "URL" with my code, the ajax won't call hit the controller.

Ajax

$(document).ready(function () {
    var expenseID = $(".expense-title span").text(); // The Id i want to pass
    table = $("#Formtable").DataTable({
        responsive: true,
        "ajax": {
            "url": "/Forms/GetForm/"   expenseID,
            dataSrc: ""
        },
        "columns": [
            {
                "data": "receipt_Date",
            },
            {
                "data": "category",
            },
            {
                "data": "receipt_Date",
            },
            {
                "data": "payee"
            },
            {
                "data": "payee",
            },
            {
                "data": "total",
            }
        ],
        success: function (result) {
            console.log(result)
        },
        error: function (error) {
            console.log(error)
        }
    });
});

Controller

        [HttpGet("{expenseid}")]
        public async Task<JsonResult> GetForm(int expenseid)
        {
            var result = await formRepository.GetForm(expenseid);
            return Json(result);
        }

CodePudding user response:

try to use the whole route attribute and remove Get

 [Route("~/forms/getform/{expenseid}")]
 public async Task<JsonResult> GetForm(int expenseid)
  • Related