How to pass values to controller from Javascript code in MVC

China☆狼群 提交于 2019-12-01 00:53:39

You can use AJAX inside your script.

Something like this

$.ajax({
         url: 'Your method in controller which will delete the records',
         type: 'POST',
         data: ' data which you want to send to controler ',
});
Instance Noodle

In your controller use this statement for retrieving the value: Request.Params["ID"]

Consider the following example i am firing the script with a list value using for loop table data.

<a href='#' class="gridEdit"  onclick="javascript:return DeleteServiceOut('@Model.EmployeeInformationList[i].GEmployeeGenInfoID.ToString()');"></a>  

the function to pass values to controller from Javascript code in MVC

<script>
 function DeleteServiceOut(GEmployeeId) {

        window.location = "/PMS/Payroll/ServiceOutAdd/ServiceOutDelete/" + GEmployeeId;
        return false;
    }

</script>

in to the controller

 public ActionResult ServiceOutDelete(string id, ServiceOutModels model)
        {
           // id will contailn the value of parameter
           // AS you want
            return View("ServiceOutViewDetails", model);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!