AjaxHelper学习,ajax,microsoft,mvc,asp.net

index.cshtml

@using (Ajax.BeginForm("ContentAjax", new AjaxOptions { UpdateTargetId = "pajax" }))
{
    <div>
        <p id="pajax"></p>
        <input class="text" name="username" />
        <button class="btn-default">ContentAjax测试</button>
    </div>
}

<div>
    @Ajax.ActionLink("JsonAjax测试", "JsonAjax", new AjaxOptions { OnSuccess = "jsonshow" })
</div>

<script>
    function jsonshow(data)
    {
        $("#pajax").html("用户ID:" + data.id + "<br/>"
            + "姓名:" + data.name + "<br/>"
            + "Email:" + data.email);
    }
</script>

<div>
    @Ajax.ActionLink("PartialViewAjax测试", "PartialViewAjax", new AjaxOptions { UpdateTargetId = "pajax" })
</div>

@section Scripts{
    @Scripts.Render("~/bundles/jqueryval")
}

HomeController.cs

public ActionResult Index()
        {
            return View();
        }

        public ActionResult ContentAjax(string username)
        {
            return Content("Content:" + "<br>" + "用户名:" + username);
        }

        public ActionResult JsonAjax()
        {
            UserInfo user = new UserInfo { id = 1, name = "Json", email = "[email protected]" };
            return Json(user, JsonRequestBehavior.AllowGet);
        }

        public ActionResult PartialViewAjax()
        {
            UserInfo user = new UserInfo { id = 2, name = "PartialView", email = "[email protected]" };
            return PartialView("PartialViewAjax",user);
        }

PartialViewAjax.cshtml

@model MvcBasic.Models.UserInfo
<p>
    用户ID:@Model.id
    <br/>
    姓名:@Model.name
    <br/>
    Email:@Model.email
</p>

Model

public class UserInfo
    {
        public int id { get; set; }

        public string name { get; set; }

        public string email { get; set; }
    }

AjaxHelper学习,ajax,microsoft,mvc,asp.net,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。