一、Model層 二、控制器層 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Mvc_Demo. ...
一、Model層
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace Mvc_Demo.Models 7 { 8 public class Person 9 { 10 public int Age { get; set; } 11 public string Name { get; set; } 12 public string Sex { get; set; } 13 } 14 }
二、控制器層
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Mvc_Demo.Models; 7 8 namespace Mvc_Demo.Controllers 9 { 10 public class PersonController : Controller 11 { 12 //==================自定義方式的自動裝配方法========================= 13 // GET: /Person/ 14 //1、通過預設的get請求方式,執行這個行為,請求到AddPerson這個view頁面,其才會展示出來 15 [HttpGet]//預設請求方式 16 public ActionResult AddPerson() 17 { 18 return View(); 19 } 20 //2、在AddPerson這個view頁面輸入值以後,點擊提交,因為指定了他們的提交方式為post方式, 21 //故提交以後,會執行此行為,然後顯示AddPerson1頁面,將數據進行展示出來 22 [HttpPost] 23 public ActionResult AddPerson(Person person) 24 { 25 ViewData.Model = person; 26 return View("AddPerson1"); 27 } 28 //===============================單個值的自定義裝配============================ 29 [HttpPost] 30 public ActionResult Purple(string dd) 31 { 32 ViewBag.DD = dd; 33 return View("Purple"); 34 } 35 } 36 }View Code
三、視圖(View)
1.處理視圖
1 @*引入命名空間*@ 2 @using Mvc_Demo.Controllers; 3 @using Mvc_Demo.Models; 4 @model Mvc_Demo.Models.Person 5 6 @{ 7 ViewBag.Title = "AddPerson"; 8 } 9 @* ========================自定義方式的自動裝配================================= *@ 10 <h2>AddPerson</h2> 11 @using (Html.BeginForm("AddPerson", "Person", FormMethod.Post)) 12 { 13 <span>年齡:</span> 14 @Html.TextBoxFor(p=>p.Age) 15 <hr /> 16 <span>姓名:</span> 17 @Html.TextBoxFor(p=>p.Name) 18 <hr /> 19 <span>性別:</span> 20 @Html.TextBoxFor(p=>p.Sex) 21 <hr /> 22 <input type="submit" name="Submit" value="提交" /> 23 } 24 @* ========================單個值的自動裝配================================ *@ 25 @using (Html.BeginForm("Purple", "Person", FormMethod.Post)) 26 { 27 @Html.TextBox("dd") 28 <input type="submit" name="name" value="提交哈" /> 29 } 30 @* ===========================跳轉鏈接============================== *@ 31 @*鏈接跳轉失敗*@ 32 <a href="@Url.Action("Purple", "Person")">跳轉到AddPerson1頁面</a> 33 <hr /> 34 @Html.ActionLink("跳轉到AddPerson1頁面", "AddPerson1", "Person") 35 <hr /> 36 @* ====================在指定位置輸出值===================================== *@ 37 @*在指定位置輸出值*@ 38 @Html.Raw("我是HTML幫助類方式輸出") 39 @*在頁面最頂端輸出值*@ 40 @{ 41 Response.Write("我是Response方式輸出"); 42 }
2.顯示視圖(1)
1 @using Mvc_Demo.Controllers; 2 @model Mvc_Demo.Models.Person 3 4 @{ 5 ViewBag.Title = "AddPerson1"; 6 } 7 @* ========================自定義方式的自動裝配================================= *@ 8 <h2>AddPerson1</h2> 9 <span>年齡:</span> 10 <h1>@Model.Age</h1> 11 <hr /> 12 <span>姓名:</span> 13 <h1>@Model.Name</h1> 14 <hr /> 15 <span>性別:</span> 16 <h1>@Model.Sex</h1> 17 <hr /> 18 19 20 21 22 @*當路由是通過動態規則方式動態生成時,通過這種方式會動態生成路徑*@ 23 @Html.ActionLink("跳轉到AddPerson頁面", "AddPerson", "Person")
3.顯示視圖(2)
1 @model Mvc_Demo.Models.Person 2 3 @{ 4 ViewBag.Title = "Purple"; 5 } 6 7 <h2>Purple</h2> 8 @* ========================單個值的自動裝配================================ *@ 9 @{ 10 //string str=Convert.ToString(@ViewBag.DD); 11 @*@Html.TextArea(str)*@ 12 @ViewBag.DD 13 }