MVC局部刷新

看一下运行的效果

当用户名为a密码为b时运行正确

当什么都不填时运行错误

HomeController的代码:

   public ActionResult Index()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult JuBushuaxin(string name, string pass)
        {
            if (name != "a" || pass != "b")
            {
                var data = "false";
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var data = "success";
                return Json(data, JsonRequestBehavior.AllowGet);
            }
           
        }

index.cshtml的代码

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<script type="text/javascript">
    $(document).ready(function () {
        $("#button").click(function () {
            //     alert("");
            var name = $("#username").val();
            var pass = $("#pass").val();
            $.getJSON(
                  "/home/JuBushuaxin", { name: name, pass: pass },
                  function (data) {
                 
                      if (data == "success") {
                          alert("用户名密码正确");
                      } else {
                          alert("用户名密码错误");
                      }

                  }
                  );
        });
        
    });
</script>
<div id="tdiv">
    
</div>
<p>用户名:<input type="text" id="username"/></p>
<p>密码:<input type="password" id="pass"/></p>
<input type="button" id="button" value="自动添加数据"/>

希望对大家有所帮助

posted @ 2012-10-24 22:57  win_and_first  阅读(839)  评论(0编辑  收藏  举报