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

本文展示了如何在ASP.NET MVC中利用Ajax进行跨页面数据交互,包括发送请求到服务器、处理服务器响应并更新页面内容。通过具体代码实例,详细介绍了使用Ajax.BeginForm和Ajax.ActionLink的方法,以及如何在客户端通过脚本处理服务器返回的数据。

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 = "json@qq.com" };
            return Json(user, JsonRequestBehavior.AllowGet);
        }

        public ActionResult PartialViewAjax()
        {
            UserInfo user = new UserInfo { id = 2, name = "PartialView", email = "PartialViewAjax@qq.com" };
            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; }
    }

转载于:https://www.cnblogs.com/bushuosx/p/3911165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值