Controllor
------------------------public ActionResult GetResult(string name)
{
    return Content("hello, " + name);
}
View
------------------------<script type="text/javascript">
    function GetMessage() {
        $.get("/Home/GetResult/", function (response) {
            $("#mySpan").html(response);
        });
    }
</script><input type="text" maxlength="8" id="characterName" />
<input type="button" onclick="GetMessage()" value="开始" />
<span id="mySpan"></span>
如上,请问如何把 characterName 传递到controllor?

解决方案 »

  1.   

    var oValue =  $("#characterName").getVal();GetResult 如果方法接受一个参数就直接把地址改成var url = "/Home/GetResult/"+oValue ;  至于具体参数放到什么位置,看GetResult 的定义如果不接受参数就用 "/Home/GetResult/"+"?name=" + oValue 的形式 在GetResult 里Request
      

  2.   


    <script type="text/javascript">
        function GetMessage() {
            $.get("/Home/GetResult/"+$("#characterName"), function (response) {
                $("#mySpan").html(response);
            });
        }
    </script>routes.MapRoute(
                    "GetMessage", // 流程附件上传
                    "{Home}/{GetResult}/{name}", // 带有参数的 URL
                    new { controller = "Home", action = "GetResult", name= UrlParameter.Optional} 
                );public ActionResult GetResult(string name)
    {
        return Content("hello, " + name);
    }
    这样需要在路由中重新配置规则,因为默认路由传递参数为id.<script type="text/javascript">
        function GetMessage() {
            $.get("/Home/GetResult/?characterName="+$("#characterName"), function (response) {
                $("#mySpan").html(response);
            });
        }
    </script>public ActionResult GetResult()
    {
        return Content("hello, " + Request.QueryString["characterName"]);
    }
      

  3.   


    hi, a1214668850
    我按你的方法配置了路由,在 default 路由的下面:routes.MapRoute(
                    "Default", // 路由名称
                    "{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "Account", action = "AccountIndex", id = UrlParameter.Optional } // 参数默认值
                );            routes.MapRoute(
                    "CheckExists", // 路由名称
                    "{controller}/{action}/{characterName}", // 带有参数的 URL
                    new { controller = "Account", action = "CheckExists", characterName = UrlParameter.Optional } // 参数默认值
                );------------ Controllor :public ActionResult CheckExists(string characterName)
    {
    return Content("hello," + characterName);
    }------------ View:
    $.get("/Account/CheckExists/" + $("#characterName").val(), function (response) {
                        if (response == "true")
                            $("#createResult").html("名称不可用,请使用其他名称。");
                        else
                            $("#form1").submit();
                    });但我断点发现,传递到 Controllor 时,characterName 为 null。这是为什么呢?
      

  4.   

    $("#characterName").val()  请求之前看下这个有没有值?
      

  5.   


    alert($("#characterName").val()); // 弹出的结果是文本框输入的内容
    $.get("/Account/CheckExists/" + $("#characterName").val(), function (response) {
       if (response == "true")
       $("#createResult").html("名称不可用,请使用其他名称。");
       else
       $("#form1").submit();
       });
      

  6.   

    通常情况都是"/Home/GetResult?name="+characterName,主义这里的参数"name"一定要跟你ActionResult的参数名称"name"是一致的