13-Ajax链接与Ajax表单的快速对比Demo

1.创建mvc4 basic类型项目

2.创建Home控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAjaxP158.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetData()
        {
            return PartialView("PartialOne");
        }
        public ActionResult GetData2()
        {
            return Content("<i>bbbb</i>");
        }
        public string LeaveMessage(FormCollection collection)
        {
            string k_Message = "标题:" + collection["yTitle"] + " Email:" + collection["yEmail"] +
                " QQ:" + collection["yQQ"] + " 内容:" + collection["yContent"];
            return k_Message;
        }      
    }
}
3.创建View,Index.cshtml内容如下:

@{
    ViewBag.Title = "Index";
}

<h2>1.Ajax链接测试</h2>
<div id="cs1"></div>
@Ajax.ActionLink("GetData","GetData",new AjaxOptions(){
    UpdateTargetId="cs1",InsertionMode=InsertionMode.InsertAfter,HttpMethod="get"})
@Ajax.ActionLink("GetData2","GetData2",new AjaxOptions(){
    UpdateTargetId="cs1",InsertionMode=InsertionMode.Replace,HttpMethod="post"})
<hr />
<h2>2.Ajax表单测试</h2>
<div id="cs2"></div>
@using (Ajax.BeginForm("LeaveMessage", new AjaxOptions() {
    HttpMethod="post",UpdateTargetId="cs2" }))
{
    <p>@Html.Label("yTitle","标题")  @Html.TextBox("yTitle") </p>
    <p>@Html.Label("yEmail", "电子邮件") @Html.TextBox("yEmail")</p>
    <p>@Html.Label("yQQ", "QQ") @Html.TextBox("yQQ")</p>
    <p>@Html.Label("yContent", "内容") @Html.TextBox("yContent")</p>
    <p><input type="submit" value="提交留言"  /></p>
    
}

@using (Ajax.BeginForm("GetData", new AjaxOptions() {
    HttpMethod="get",UpdateTargetId="cs2" }))
{   
    <p><input type="submit" value="提交"  /></p>    
}
4.在View/Home下面创建一个PartialOne局部视图,内容如下:

<i>aaaa</i>

5.在_Layout中添加一行代码: @Scripts.Render("~/bundles/jqueryval").文件内容如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    @RenderSection("scripts", required: false)
</body>
</html>

最终界面效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值