Asp.Net MVC Control向View传值

 1、通过View(Parameter)参数传值    

Control:

namespace MyMVCDemo.Controllers
{
    public class PersonController : Controller
    {
        //
        // GET: /Person/
        public ActionResult Index()
        {
            Person whiteperson = new Person();
            whiteperson.Id = 1;
            whiteperson.Name = "James";
            whiteperson.Age = 28;

            return View(whiteperson);
        }

      }
}

View

@model MyMVCDemo.Models.Person   //强类型视图
@using MyMVCDemo.Models//引用实体类命名空间
@{var person = ViewData.Model;}

<br />
<p>@person.Id</p>
<p>@person.Name</p>

2、通过动态对象ViewBag.key传值,ViewBag 是动态类型(dynamic),在程序运行时动态解析

Control

namespace MyMVCDemo.Controllers
{
    public class PersonController : Controller
    {
        //
        // GET: /Person/
        public ActionResult Index()
        {
            Person whiteperson = new Person();
            whiteperson.Id = 1;
            whiteperson.Name = "James";
            whiteperson.Age = 28;

            ViewBag.person = whiteperson;


            return View();
        }

      }
}

  

View

@using MyMVCDemo.Models
@{var person = ViewBag.person;}

    <br />
    <p>@person.Id</p>
    <p>@person.Name</p>

3、通过ViewData传值,是一个字典型,数据是Object类型,使用时要进行强制类型转换

Control

namespace MyMVCDemo.Controllers
{
    public class PersonController : Controller
    {
        //
        // GET: /Person/
        public ActionResult Index()
        {
            Person whiteperson = new Person();
            whiteperson.Id = 1;
            whiteperson.Name = "James";
            whiteperson.Age = 28;

            ViewData["person"] = whiteperson;


            return View();
        }

      }
}

View

@using MyMVCDemo.Models  
@{var person =(Person)ViewData["person"]
;} <br /> <p>@person.Id</p> <p>@person.Name</p>

4、通过TempData传值

Control

namespace MyMVCDemo.Controllers
{
    public class PersonController : Controller
    {
        //
        // GET: /Person/
        public ActionResult Index()
        {
            Person whiteperson = new Person();
            whiteperson.Id = 1;
            whiteperson.Name = "James";
            whiteperson.Age = 28;

            TempData["id"] = whiteperson.Id;
            TempData["name"] = whiteperson.Name;
            TempData["age"] = whiteperson.Age;


            return View();
        }

      }
}

View

    <label>@TempData["id"]</label>
    <label>@TempData["name"]</label>
    <label>@TempData["age"]</label>

5、总结

ViewData是字典类型的,数据是Object类型的,因此使用时要进行强制类型转换;ViewBag是dynamic类型,在程序运行时动态解析;ViewData和ViewBag的生命周期与View相同,并且只对当期View有效;而TempData可以在不同的View中进行传值,是用session机制处理的,但是TempData里面的数据在访问了一次后会被清除。

Control

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

namespace MyMVCDemo.Controllers
{
    public class PersonController : Controller
    {
        //
        // GET: /Person/
        public ActionResult Index()
        {
            Person whiteperson = new Person();
            whiteperson.Id = 1;
            whiteperson.Name = "James";
            whiteperson.Age = 28;
            //ViewData向视图传递数据
            ViewData["id"] = whiteperson.Id;
            ViewData["name"] = whiteperson.Name;
            ViewData["age"] = whiteperson.Age;
            ViewData["person"] = whiteperson;
            //ViewBag向视图传递数据
            ViewBag.id = whiteperson.Id;
            ViewBag.name = whiteperson.Name;
            ViewBag.age = whiteperson.Age;
            ViewBag.person = whiteperson;
            //TempData向视图传递数据
            TempData["id"] = whiteperson.Id;
            TempData["name"] = whiteperson.Name;
            TempData["age"] = whiteperson.Age;
            TempData["temp"] = "TempData";
            ViewBag.temp = "ViewBagData";
            ViewData["temp"] = "ViewData";
            //return View(whiteperson);
            return View(whiteperson);
        }

        public ActionResult About()
        {
            return View();
        }
	}
}

About视图里面调用:

@{
    ViewBag.Title = "About";
}

<h2>About</h2>
<div>@TempData["temp"]</div>
<div>@ViewData["temp"]</div>
<div>@ViewBag.temp</div>

技术分享

刷新About页面,则TempData["temp"]数据删除

技术分享

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