AspNet MVC4 教学-14:Asp.Net MVC4 ViewBag等数据传输技术快速比较Demo

新建一个Basic类型的Project.

1.HomeCtroller.cs:

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

namespace MvcViewBagTest.Controllers
{
    public class HomeController : Controller
    {
        //GET: /Home/
        public ActionResult Index()
        {
            ViewBag.cs1 = "Index-ViewBag";
            ViewData["cs2"] = "Index-ViewData";
            TempData["cs"] = "Index-TempData";
            Session["cs"] = "Index-Session";
            return View();
        }
        public ActionResult Index2()
        {
            return View();
        }
        public ActionResult Index3()
        {
            return View();
        }
    }
}

2.遇上面对应的3个相应的View文件:

Index.cshtml:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<div>@ViewBag.cs1</div>
<div>@ViewData["cs2"]</div>
@*<div> @TempData["cs"]</div>*@
<div>@Session["cs"]</div>
@Html.ActionLink("Index2", "Index2")
@Html.ActionLink("Index3", "Index3")

Index2.cshtml:

@{
    ViewBag.Title = "Index2";
}

<h2>Index2</h2>
<div>@ViewBag.cs1</div>
<div>@ViewData["cs2"]</div>
<div> @TempData["cs"]</div>
<div> @Session["cs"]</div>
@Html.ActionLink("Index3","Index3")
@Html.ActionLink("Index","Index")

Index3.cshtml:

@{
    ViewBag.Title = "Index3";
}

<h2>Index3</h2>
<div>@ViewBag.cs1</div>
<div>@ViewData["cs2"]</div>
<div> @TempData["cs"]</div>
<div>@Session["cs"]</div>
@Html.ActionLink("Index2","Index2")
@Html.ActionLink("Index","Index")


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