ASP.NET MVC2 in Action 读书笔记 [12-4] MVC Ajax Helpers

ASP.NET AJAX 示例
本文介绍了一个使用ASP.NET AJAX实现评论添加功能及隐私政策显示的例子。通过BeginForm进行异步提交,展示如何使用ActionLink更新页面内容。

Index.aspx:

<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
 
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
 
 
<h3>Ajax.BeginForm</h3>
    
    <h4>Comments</h4>    
    <ul id="comments">        
    </ul>
    
    <% using(Ajax.BeginForm("AddComment", new AjaxOptions
                                            {
                                                HttpMethod = "POST", 
                                                UpdateTargetId = "comments",
                                                InsertionMode = InsertionMode.InsertAfter                                                
                                            })) { %>
    
        <%= Html.TextArea("Comment", new{rows=5, cols=50}) %>
        <button type="submit">Add Comment</button>
                                            
    <% } %>
    
    <h3>Ajax.Link</h3>
    
    <%= Ajax.ActionLink("Show the privacy Policy", "PrivacyPolicy", 
        new AjaxOptions{InsertionMode = InsertionMode.Replace, UpdateTargetId = "privacy"}) %>
 
    <div id="privacy"></div>

 

AjaxHelpersController.cs:

public class AjaxHelpersController : Controller
    {
        private IList<string> _comments = new List<string>();
 
        public ActionResult Index()
        {
            return View(_comments);
        }
 
        [HttpPost]
        public ActionResult AddComment(string comment)
        {
            _comments.Add("<li>" + comment + "</li>");
            return Content(string.Join("\n", _comments.ToArray()));
        }
 
        public ActionResult PrivacyPolicy()
        {
            const string privacyText = @"
                <h2>Our Commitment To Privacy</h2>
                Your privacy is important to us. To better protect your privacy we provide this notice explaining our online 
                information practices and the choices you can make about the way your information is collected and used. 
                To make this notice easy to find, we make it available on our homepage and at every point where personally 
                identifiable information may be requested.";
 
            return Content(privacyText, "text/html");
        }
    }

转载于:https://www.cnblogs.com/RobotTech/archive/2011/08/04/2126953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值