如何用JQuery将View中的值Post到Controller

有时候我们需要动态的将View中的值post 到Controller中做一定的处理,比如ToolTip,下面是JQuery代码:

    $(document).ready(function () {
        var title = ""
        var toolTip = $("<p id=\"toolTipCss\"></p>")
                
        $(".Branch").bind("mouseover mouseout", function (event) {
            if (event.target.className.toLowerCase() === "branch") {
                var runID = $(this).parent($(this)).find("td:first").text();
                toolTip.appendTo("body").hide();
                if (event.type == "mouseover") {
                    $.post("/RunDisplay/BranchInfo", { runID: JSON.stringify(runID) },
                        function (data) {
                            toolTip.html("<p style=\"background-color:gray;\" class=\"toolTipText\">" + JSON.stringify(data) + "</p>").show().css({
                                "top": event.clientY - (document.body.scrollTop),
                                "left": event.clientX  - (document.body.scrollLeft) + 20,
                                "position": "fixed",

                                opacity: 0
                            }).animate(
                                {
                                    left: "+=10",

                                    opacity: 1

                                }, "5000");
                            }
                        )
                }
                if (event.type == "mouseout") {

                    runID= "";

                    toolTip.animate(

                        {
                            opacity: 0
                        }, "5000");
                }
            }
        });
    });

 值得注意的是:

  1. $.post方法中传入到Controller的Data(第二个参数)名字必须与Controller的参数名一致。
  2. 使用Post方法时,我们使用"/RunDisplay/BranchInfo"的方式指定Controller,即:/控制器名/动作名。

下面是相应的Action:

        [HttpPost]
        public ActionResult BranchInfo(string runID )
        {
    
            //一些操作。。。                
            return Json(runID .ToString());
        }
原文地址:https://www.cnblogs.com/FsharpZack/p/2993606.html