ASP.NET MVC入门(二)---MVC数据传递

来源:互联网 发布:编程时间和水平成正比 编辑:程序博客网 时间:2024/06/06 04:50

先来看一个简单的从Model到View传递数据的例子。

1、Model

       在Models文件夹下新建一个类:

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Demo02_MVC数据传递.Models{    public class Man    {        public string name { get; set; }        public int age { get; set; }        public Man(string NAME, int AGE)        {            this.name = NAME;            this.age = AGE;        }    }}
2、Controller

       在Contrllers文件下新建一个类:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Demo02_MVC数据传递.Models;namespace Demo02_MVC数据传递.Controllers{    public class TestController : Controller    {        public ActionResult Index()        {            return View();        }        public string sayHello()        {            return "Hello GeoStorm!";        }        /// <summary>        /// 用ViewData传递简单对象方法一        /// </summary>        /// <returns></returns>        public ActionResult GetView()        {            Man m = new Man("张三",20);            ViewData["Man"] = m;          //用ViewData进行封装Man对象            return View("MyView");        }        /// <summary>        /// 用ViewData传递简单对象方法二        /// </summary>        /// <returns></returns>        public ActionResult GetView3()        {            Man m = new Man("张三", 20);            ViewData["Man"] = m;                     return View("MyView3");//在试图中第一行添加引用代码时,需要两个参数的View。但是两个参数的View不一定在视图中第一页需要添加引用!!!!!!        }        /// <summary>        /// 传递List<Man>集合        /// </summary>        /// <returns></returns>        public ActionResult GetView2()        {            List<Man> m = new List<Man>()            {                new Man("张三",20),                new Man("李四",22)            };            ViewData["Man"] = m;            return View("MyView2");        }    }}
3、View

(1)添加第一个View:

       在动作名GetView()上右键→添加视图,命名为MyView,点击确定。


代码修改如下:

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>MyView</title></head><body>    <div>        @{            //将ViewData封装强制转换,拆封为类对象            Demo02_MVC数据传递.Models.Man m = (Demo02_MVC数据传递.Models.Man)ViewData["Man"];        }        @m.name    </div></body></html>

运行效果:


(2)MyView2

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>MyView2</title></head><body>    <div>        @{            List<Demo02_MVC数据传递.Models.Man> L=(List<Demo02_MVC数据传递.Models.Man>)ViewData["Man"];        }        @{            foreach(Demo02_MVC数据传递.Models.Man m in L)            {                Response.Write(m.name+"-"+m.age);            }        }    </div></body></html>

运行效果:


(3)MyView3

@*在试图中第一行添加引用代码时,需要两个参数的View。但是两个参数的View不一定在视图中第一页需要添加引用!!!!!!*@@model Demo02_MVC数据传递.Models.Man@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>MyView3</title></head><body>    <div>        @Model.name    </div></body></html>

运行效果:







0 0
原创粉丝点击