zoukankan      html  css  js  c++  java
  • Controller向View传值方式

    我们一般会向页面传递一个主要的类型的数据,一般情况下是一个List<Model>
    ViewBag  ViewData  TempData之间的差别
    ViewData与ViewBag之间非常相似,二者使用场景基本一致,但是ViewData的类型时很明确的,而ViewBag的类型时动态的,直接就可以使用,他们的传值范围是:controller向view传值,view自己和自己传值。而TempData存在的目的就是为了防止redirect的时候数据丢失,所以他的传值范围就是当前controller和跳转后的controller之间。
     
    具体用法:
    ViewBag:
    ViewBag = ViewData+Dynamic wrapper around the ViewData
    1.controller向view传值
    view:
      <p>@ViewBag.Message</p>
    controller:
      public ActionResult Index()
      {
        ViewBag.Message = "Dude,it is very Simple"
        return View();
      }
    2.view向view传值(同一个view之间传值)
      @{
        ViewBag.Title = "Index";
      }
      <p>@ViewBag.Title</p>
    3.viewBag传递一个对象
    View:
      <ul>
        @foreach(var std in ViewBag.Students)
        {
          <li>@std</li>
        }
      </ul>
    Controller:
      public ActionResult Index()
      {
        var student = new List<string>
        {
          "Deepak Kumar",
          "Rohit Ranjan",
          "Manish Kumar"
        };
        ViewBag.Students = students;
        return View();
      }
     
    Viewdata:
    1.viewdata基本用法
    View:
      <ul>
        @<foreach(var std in(List<string>ViewData["Students"])
         {
          <li>@std</li>
         }
      </ul>
    controller:
      public ActionResult Index()
      {
        var students = new List<string>
        {
          "Deepak Kumar",
          "Rohit Ranjan",
          "Manish Kumar"
        };
        ViewData["Students"] = students;
        return View();
      }
    ViewData转换成ViewBag:
    View:
      <ul>
        @foreach(var std in(List<string>)ViewBag.Students)
        {
          <li>@std</li>
        }
      </ul>
    Controller:
      public ActionResult Index()
      {
        var students = new List<string>
        {
          "Deepak Kumar",
          "Rohit Ranjan",
          "Manish Kumar"
        };
        ViewData["Students"] = students;
        return View();
      }
    ViewBag转换成ViewData:
    View:
      <ul>
        @foreach(var std in(List<string>)ViewData["Students"])
        {
          <li>@std</li>
        }
      </ul>
    controller:
      public ActionResult Index()
      {
        var students = new List<string>
        {
          "Deepak Kumar",
          "Rohit Ranjan",
          "Manish Kumar"
        }
        ViewBag.Students = students;
        return View();
      }
    TempData:
    TempData用于在Redirect的时候保存数据,而ViewData和ViewBag在跳转后就会变成null
      public ActionResult Index()
      {
        var model = new Review()
        {
          Body  = "Start",
          Rating = 5
        };
        TempData["ModelName"] = model;
        return RedirectToAction("About");
      }
      <pre><pre lang="cs">public ActionResult About()
      {
        var model = TempData["ModelName"];
        return View(model);
      }
    普通页面传递model:
    controller:
      public ActionResult Index()
      {
          Product p = new Product();
          p.Name = "Toy";
          return View(p);
      }
    View:
      Product : <%: ((Product)Model).Name %>
    向强类型视图传递model:
    controller:
      public ActionResult Index()
      {
          Product p = new Product();
          p.Name = "Toy";
          return View(p);
      }
    View:
    声明类型
      <%@ Page Inherits="System.Web.Mvc.ViewPage<Product>" %>
    直接用Model调用该对象
      <h2> Product Name: <%: Model.Name %> </h2>
    Razor视图引擎:
    在razor中申明类型的方式:@model MVC3App.Models.Product
    在razor中调用对象的方式:<h2>Product:@Model.Name</h2>

  • 相关阅读:
    C# WinForm 关于窗体最大化时的是否全屏效果与是否遮盖任务栏
    C# winform 无边框 窗体的拖动
    lenovo 联想笔记本ideapad 320c-15改装win7问题
    解决WIN7第一次开机冷启动QQ未响应的办法
    WIN10X64_LTSB2016极限精简版by双心
    RAMOS和SSD对比
    联想IDEAPAD 320C-15笔记本显卡驱动问题
    WIN7以上系统安装VB6的解决办法,附上个批处理。
    吐槽下银联1分钱乘公交
    QQ聊天框变成方框口口口口的解决办法
  • 原文地址:https://www.cnblogs.com/HansZimmer/p/8821581.html
Copyright © 2011-2022 走看看