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>

  • 相关阅读:
    python学习笔记
    Jenkins 共享库最佳实践
    django form 表单
    cenos7搭建openldap双主+keepalived+tls
    jenkins+ansible+gitlab
    centos7搭建zabbix3.4.13
    HTTP八种响应方式
    django administration 小记
    django manage.py 详解
    Django settings详解
  • 原文地址:https://www.cnblogs.com/HansZimmer/p/8821581.html
Copyright © 2011-2022 走看看