控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace templates.Views.test2
{
public class testController : Controller
{
/// <summary>
/// 1、提供視圖模型對象
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
DateTime date = DateTime.Now;
//一個對象作為View方法的參數(shù)傳遞給視圖
return View(date);
}
/// <summary>
/// 2、使用ViewBag(視圖包)傳遞數(shù)據(jù)
/// </summary>
/// <returns></returns>
public ActionResult Edit()
{
ViewBag.Name = "張三";
ViewBag.Age = "46";
ViewBag.Hobby = "運動,下象棋";
return View();
}
/// <summary>
/// 3、 使用View Data傳遞數(shù)據(jù)
/// </summary>
/// <returns></returns>
public ActionResult add()
{
ViewData["Message"] = "大家好";
ViewData["Date"] = DateTime.Now;
return View();
}
//使用TempDate來傳遞數(shù)據(jù)
public ActionResult detail()
{
//TempData 他和viewdate的用法差不多但是他比viewdate的好處是他可以做到夸不同頁面之間的傳值
TempData["Message"] = "名字信息";
TempData["Age"] = "55";
TempData["Hobby"] = "打籃球,打羽毛球等";
TempData["Date"] = DateTime.Now;
return View();
}
}
}
視圖:
添加頁面:
@{
ViewBag.Title = "add";
}
<h2>add</h2>
<div>
the day is:@(((DateTime)ViewData["Date"]).ToString("yyyy-MM-dd"))
<br />
消息:@(ViewData["Message"]+"")
</div>
修改頁面:
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<div>
名字:@ViewBag.Name
年齡:@ViewBag.Age
愛好:@ViewBag.Hobby
</div>
首頁:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
the day is:@(((DateTime)Model).ToString("yyyy-MM-dd"))
</div>
詳細頁面:
@{
ViewBag.Title = "detail";
}
<h2>detail</h2>
<div>
詳細頁面日期:@(((DateTime)TempData["Date"]).ToString("yyyy-MM-dd"))
<br />
消息:@(TempData["Message"] + "")
</div>
聯(lián)系客服