Passing id value of actionlink to javascript function in my Asp.net Mvc Project

676 views Asked by At

I have this actionlink in my _alyout.cshtml:

@Html.ActionLink("linky", "action", "controller", new { id = 1})

and this Java script in my Index.cshtml.

<script type="text/javascript">

    $(document).ready(function () {

        getProductsByDepartmentId(departmentid);

    })


function getProductsByDepartmentId(departmentid) {

    var tbl = $('#productTable');
    $.ajax({
        cache: false,
        url: '/Home/GetProductsByDepartmentId?id=' + departmentid,
        contentType: 'application/html ; charset:utf-8',
        type: 'GET',
        dataType: 'html',
        success: function (result) {
            tbl.empty().append(result);
        },
        error: function () {

        }

    });

    }

My Action method:

[HttpGet]
        public async Task<ActionResult> GetProductsByDepartmentId(int id)
        {
            using (context)
            {
                return (PartialView("_ProductList", await context.Products.Where(d => d.Deptid == id).ToListAsync()));
            }

        }

How do I do to call my javascriptfunktion from my actionlink. I do I pass value of id in Actionlink to my getProductsByDepartmentId(departmentid) function? I would be grateful for any help

0

There are 0 answers