@model MvcApplication1.Models.M_Person
@using MvcApplication1.Models;
@{
    ViewBag.Title = "GetData";
    var p = ViewData["data"] as M_Person;
    var p2 = ViewBag.Data as M_Person;
}
<h2>
    GetData</h2>
<div>
    这是从ViewData.Model中取出的数据 @ViewData.Model.Name
</div>
<div>
    这是从ViewData["data"]中取出的数据 @p.Age
</div>
<div>
    这是从ViewBag.Data中取出的数据 @p2.Name @p2.Age
</div>
@{int i = 1;}
@*@helper ChangeColor(int age)
    {
    if (age > 90)
    {

    <font color="red">@age</font>
    }
    else
    {
    @age
    }
}*@
@*@functions{

public IHtmlString ChangeColor(int age)

    {

if(age>90)

        {

return new HtmlString("<font color='red'>"+age+"</font>");

        }else

        {

return new HtmlString(age + "");

        }

    }

}*@
<table border="1px" cellpadding="2px" cellspacing="0px" width="500px" style="border-collapse: collapse">
    <tr>
        <th width="20px">
            ID
        </th>
        <th>
            和尚
        </th>
        <th width="50px">
            年龄
        </th>
        <th width="100px">
            操作
        </th>
    </tr>
    @foreach (M_Person person in ViewBag.Persons)
    { 

        <tr>
            <td align="center">@(i++)
            </td>
            <td align="center">@person.Name
            </td>
           @* <td align="center">@ChangeColor(person.Age)*@
          @* <td align="center">@UIHelper.ChangeColor(person.Age)*@
         @* <td align="center">@ChangeColor(person.Age)</td>*@
         <td align="center">@UIFunctions.ChangeColor(person.Age)</td>
            <td align="center">
                删除||编辑
            </td>
        </tr>

    }
</table>

  UIHelper.cshtml

@helper ChangeColor(int age)
{

if(age>90)
    {
<font color="red">@age</font>
    }else
    {
@age

    }
}

  UIFunctions.cshtml

@functions{

    public static IHtmlString ChangeColor(int age)
    {

        if (age > 90)
        {

            return new HtmlString("<font color='red'>" + age + "</font>");

        }
        else
        {

            return new HtmlString(age + "");

        }

    }

}

  controller

      public ActionResult GetData()
        {
            M_Person person = new M_Person() { Name = "济公活佛", Age = 90 };
            ViewData["data"] = person;
            ViewData.Model = person;
            ViewBag.Data = person;
            List<M_Person> list = new List<M_Person>() {
            new Models.M_Person() { Name = "济公活佛", Age = 90 },
            new Models.M_Person() { Name = "广亮和尚", Age = 88 },
            new Models.M_Person() { Name = "怄气禅师", Age = 45 },
            new Models.M_Person() { Name = "飞龙僧", Age = 123 }
            };
            ViewBag.Persons = list;
            return View();
        }

  

View从Action中获得数据和html helper function(转载)的更多相关文章

  1. 一步步学习ASP.NET MVC3 (5)——View从Action中获得数据

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们把Razor的模板技术给大家介绍了一下,当然模板中还有其他的知识点,这个以后我们还会继续讲解.本章我们主要讨论 ...

  2. ASP.NET MVC中如何以ajax的方式在View和Action中传递数据

    前言:写这篇随笔的时候,在url上漏写了斜线,找了好久错误,整个人都很不好.#我是猪系列 背景:之前介绍过一篇如何构建 MVC&AJax&JSon示例,这一篇单独讲解如何在View和A ...

  3. Struts2获取Action中的数据

    当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...

  4. view如何从action中取得数据和 Html辅助方法

    方式:1使用弱类型取,2,使用强类型,两者的差别在于view页面最上方声明的方式   如果使用弱类型接受来自控制器的数据,在view页面里完全不需要有任何的生命,数据可以从ViewData,ViewB ...

  5. ACTION中获得数据的几种方式

    1.第一种是通过公司封装的方法. 2.第二种:是通过IF方法判断 3.第三种是通过:set/get获得

  6. action中json的应用

    这篇文章重点介绍action中json数据的返回处理:假设须要看前端代码的一些特效或ajax的json接收,请看上一篇博客:http://blog.csdn.net/yangkai_hudong/ar ...

  7. 将Controller中的数据传递到View中显示

    如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...

  8. struts2:数据校验,通过Action中的validate()方法实现校验,图解

    根据输入校验的处理场所的不同,可以将输入校验分为客户端校验和服务器端校验两种.服务器端验证目前有两种方式: 第一种 Struts2中提供了一个com.opensymphony.xwork2.Valid ...

  9. struts2:数据校验,通过Action中的validate()方法实现校验(续:多业务方法时的不同验证处理)

    前文:struts2:数据校验,通过Action中的validate()方法实现校验,图解 如果定义的Action中存在多个逻辑处理方法,且不同的处理逻辑可能需要不同的校验规则,在这种情况下,就需要通 ...

随机推荐

  1. js命名空间的使用

    js命名空间的使用: test.html 代码如下: <!DOCTYPE HTML><html lang="en-US"><head>    & ...

  2. Tensorflow的CNN教程解析

    之前的博客我们已经对RNN模型有了个粗略的了解.作为一个时序性模型,RNN的强大不需要我在这里重复了.今天,让我们来看看除了RNN外另一个特殊的,同时也是广为人知的强大的神经网络模型,即CNN模型.今 ...

  3. 通过focusInEvent和eventFilter两种方法改写控件颜色(自定义控件就是这么来的)

    http://www.cnblogs.com/hicjiajia/archive/2012/05/30/2526768.html http://www.cnblogs.com/hicjiajia/ar ...

  4. 开源项目之Android Afinal框架

    项目如图: 本文参考网络! Afinal是一个开源的android的orm和ioc应用开发框架,其特点是小巧灵活,代码入侵量少.在android应用开发中,通过Afinal的ioc框架,诸如ui绑定, ...

  5. When Is Cheryl's Birthday

    大早上起来逛微博,看见@西瓜大丸子汤Po的一个逻辑题,遂点开看之... 原文链接:http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.i ...

  6. Android Fragment 嵌套使用报错

    在新的SDK每次创建activity时,会自己主动生成  <pre name="code" class="java">public static c ...

  7. Ubuntu_16.04_Lamp

    Ubuntu_16.04安装Lamp开发环境 目录 安装Apache2 安装php5 安装mysql-server 安装php5-mysql(php使用mysql服务,包括mysql,mysqli,m ...

  8. 获取当前WEB应用全路径

    <%String path = request.getContextPath();String basePath =request.getScheme()+"://"+req ...

  9. JS封装移动端触摸滑动插件应用于导航banner【精装版】

    自己封装了一个小插件,一个小尝试. 可用于类似于京东导航等效果,效果多样化,很方便. 欢迎大家提点意见. mrChenSwiper(  {parent, child, type, parentN, c ...

  10. Java学习之内部类

    示例1: package com.swust.面向对象; class Person1{ private String username="zhangsan"; public Per ...