zoukankan      html  css  js  c++  java
  • ViewBag、ViewData使用

    1、Example - Using ViewData

    Controller 

    public ActionResult Index()
    {
        List<string> colors = new List<string>();
        colors.Add("red");
        colors.Add("green");
        colors.Add("blue");            

        ViewData["listColors"] = colors;
        ViewData["dateNow"] = DateTime.Now;
        ViewData["name"] = "Hajan";
        ViewData["age"] = 25;

        return View();
    }


    View (ASPX View Engine)

    <p>
        My name is 
        <b><%: ViewData["name"%></b>
        <b><%: ViewData["age"%></b> years old.
        <br />    
        I like the following colors:
    </p>
    <ul id="colors">
    <% foreach (var color in ViewData["listColors"as List<string>){ %>
        <li>
            <
    font color="<%: color %>"><%: color %></font>
        </
    li>
    <% } %>
    </ul>
    <p>
        <%: ViewData["dateNow"%>
    </p>

    (I know the code might look cleaner with Razor View engine, but it doesn’t matter right? ;) ) 

    2、Example - Using ViewBag

    Controller 

    public ActionResult Index()
    {
        List<string> colors = new List<string>();
        colors.Add("red");
        colors.Add("green");
        colors.Add("blue");

        ViewBag.ListColors = colors; //colors is List
        ViewBag.DateNow = DateTime.Now;
        ViewBag.Name = "Hajan";
        ViewBag.Age = 25;
        return View(); 
    }

    You see the difference?

    View (ASPX View Engine)

    <p>
        My name is 
        <b><%: ViewBag.Name %></b>
        <b><%: ViewBag.Age %></b> years old.
        <br />    
        I like the following colors:
    </p>
    <ul id="colors">

    <% foreach (var color in ViewBag.ListColors) { %>
        <li>
            <font color="<%: color %>"><%: color %></font>
        </li>
    <% } %>

    </ul>
    <p>
        <%: ViewBag.DateNow %>
    </p>

  • 相关阅读:
    【笔记】Nios II PIO的说明与双向操作注意点
    【笔记】C++ 类与对象
    【连载】 FPGA Verilog HDL 系列实例矩阵键盘接口
    【笔记】C++ 指针和引用的使用
    【笔记】C++ 多态性
    VB 串口通信 MSComm控件的使用
    【笔记】C++ 继承
    【连载】 FPGA Verilog HDL 系列实例乐曲演奏
    AJAX Conrtol Toolkit——HoverMenuExtender(停靠菜单)
    AJAX Control Toolkit DropDown
  • 原文地址:https://www.cnblogs.com/easypass/p/2248950.html
Copyright © 2011-2022 走看看