MVC 简单的POST局部刷新

        愉快的一天又开始,今天新建了一个MVC4的项目,表示并不知道MVC几和几的区别,对于我这种人来说,经理说用什么就用什么呗,每人要求你程序必须怎么写,不管你用什么方式,他们要的只是效果,是完成的项目。因此,新建立了一个MVC的项目,在页面上写了个小的POST方法,让它实现局部刷新

@{
    ViewBag.Title = "Home Page";
}
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    function addtxt() {
        var un = $("#userName").val();
        var p = $("#pwd").val();
        if (un==null || un == "") {
            alert("请重新填写用户名");
        }
        else if (p == "")
        {
            alert("您忘记设置密码了!");
        }
        else
        {
            $.post("/Home/addTxt", { userName: un, pwd: p }, function (data) {
                $("#newUser").append(data);
            }
            )
        }
    }
</script>

<div>
    <input type="text" id="userName" />
    <input type="password" id="pwd" /><br />
    <input type="button" id="addTxt" onclick="addtxt()" value="添加" />
</div>
<div>
    <ul id="newUser"></ul>

</div>
以上是前台的程序,用兴趣的可以复制过去看看效果,别问我为什么JS和JQ混写,我也不晓得。自己心血来潮。
 public ActionResult Index()
        {
            //ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            return View();
        }
        [HttpPost]
        public string addtxt(string userName,string pwd)
        {
            string sr="";
            sr += "<li>用户名:"+userName+ "密码:"+pwd+"</li>";
            return sr;
        }



猜你喜欢

转载自blog.csdn.net/qq_14840819/article/details/46813219