how to add jaquery data id to C# function in razor View. RRS feed

  • Question

  • User-19694853 posted

    i have code like this.

     "columns": [
                        { "data": "Id" },
                            "data": "Id", "width": "30px", "render": function (data) {
                                @if (HttpContext.Current.User.Identity.IsAuthenticated && ViewContext.Controller.IsSysAdmin() && ACCes.Common.Utils.CheckInputInvoice(User.Identity.Name,@:data) == true)
                                   @:return '<a  onclick="Edit(' + data + ')"' + " id='btnEditinline" + data + "' class='  btn btn   fa fa-pencil' title='Edit'></a>"
                                     @:return '<a ' + " id='btnEditinline" + data + "' class='  btn btn   fa fa-lock' title='Lock'></a>"

    ACCes.Common.Utils.CheckInputInvoice(User.Identity.Name,@:data) == true) => in @:data how to add id from jquery script

    thanks many

    Thursday, March 21, 2019 6:46 AM

All replies

  • User475983607 posted

    JavaScript runs in the browser after the Razor code runs on the server.  If "data" is a JavaScript variable then this approach will not work.

    Try to move the logic to the controller and passing a model that has the data needed to populate the View HTML.

    Thursday, March 21, 2019 9:28 AM
  • User1520731567 posted

    Hi zjm_zjm,

    how to add jaquery data id to C# function in razor View.

    1.If you would like to pass data from controller to view,I suggest you could define ViewBag in controller and then call it in razor view,

    For example:


    public ActionResult Index()
    return View();




    2.If you want pass data from view to controller,I suggest you could use Ajax(),

    For example:


                        type: "POST",
                        url: "/Home/AjaxMethod",
                        data: '{name: "' + $("#txtName").val() + '" }', //you could pass parameters from view to controller
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);


        public JsonResult AjaxMethod(string name)
    ...do sth according to name... PersonModel person = new PersonModel { Name = name, DateTime = DateTime.Now.ToString() };
    ... return Json(person);//then back to the success callback in ajax }

    More details about jQuery AJAX,you could refer to:


    Best Regards.

    Yuki Tao

    Friday, March 22, 2019 4:41 AM