View从Action中获得数据和html helper function(转载)
@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(转载)的更多相关文章
- 一步步学习ASP.NET MVC3 (5)——View从Action中获得数据
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们把Razor的模板技术给大家介绍了一下,当然模板中还有其他的知识点,这个以后我们还会继续讲解.本章我们主要讨论 ...
- ASP.NET MVC中如何以ajax的方式在View和Action中传递数据
前言:写这篇随笔的时候,在url上漏写了斜线,找了好久错误,整个人都很不好.#我是猪系列 背景:之前介绍过一篇如何构建 MVC&AJax&JSon示例,这一篇单独讲解如何在View和A ...
- Struts2获取Action中的数据
当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...
- view如何从action中取得数据和 Html辅助方法
方式:1使用弱类型取,2,使用强类型,两者的差别在于view页面最上方声明的方式 如果使用弱类型接受来自控制器的数据,在view页面里完全不需要有任何的生命,数据可以从ViewData,ViewB ...
- ACTION中获得数据的几种方式
1.第一种是通过公司封装的方法. 2.第二种:是通过IF方法判断 3.第三种是通过:set/get获得
- action中json的应用
这篇文章重点介绍action中json数据的返回处理:假设须要看前端代码的一些特效或ajax的json接收,请看上一篇博客:http://blog.csdn.net/yangkai_hudong/ar ...
- 将Controller中的数据传递到View中显示
如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...
- struts2:数据校验,通过Action中的validate()方法实现校验,图解
根据输入校验的处理场所的不同,可以将输入校验分为客户端校验和服务器端校验两种.服务器端验证目前有两种方式: 第一种 Struts2中提供了一个com.opensymphony.xwork2.Valid ...
- struts2:数据校验,通过Action中的validate()方法实现校验(续:多业务方法时的不同验证处理)
前文:struts2:数据校验,通过Action中的validate()方法实现校验,图解 如果定义的Action中存在多个逻辑处理方法,且不同的处理逻辑可能需要不同的校验规则,在这种情况下,就需要通 ...
随机推荐
- C++中的栈和队列
使用标准库的栈和队列时,先包含相关的头文件 #include<stack> #include<queue> 定义栈如下: stack<int> stk; 定义队列如 ...
- 根据不同需求跳转不同Activity的另外一种写法
代码如下: /* Android Asynchronous Http Client Sample Copyright (c) 2014 Marek Sebera <marek.sebera@gm ...
- Python中初始化的问题以及注释问题
#coding=utf-8 # __author__ = 'minmin' from Tkinter import * #创建一个基于Frame的Application类 class Applicat ...
- zoj 2071 Technology Trader 最大权闭合子图
传送门 和上一题一样, 也是一个最大权闭合子图.不过建图好麻烦的感觉 写了好久. 源点和原材料连边, 权值为val. 汇点和产品连边, 权值为val. 产品与和它有关系的材料连边, 权值inf. 最 ...
- Net Core- 配置组件
Net Core- 配置组件 我们之前写的配置都是放置在配置文件Web.config或者app.config中,.net core提供了全新的配置方式,可以直接写在内存中或者写在文件中. .Net C ...
- 使用Fiddler解析WCF RIA Service传输的数据
原文 http://www.cnblogs.com/wintersun/archive/2011/01/05/1926386.html 使用Fiddler 2 解析WCF RIA Service传输的 ...
- VM 映像
让我们一起欢呼吧!随着最近Microsoft Azure运行时的发布,我们非常高兴地宣布发布 OS映像的继承性产品:新 VM映像.等一下-有些人可能会觉得这听起来有点耳熟.没错,一个月前在旧金山 ...
- CentOS ips bonding
centos ip bonding 一个网卡多个ips,多个网口一个ip 1,配置一个网卡多ips的情况cp /etc/sysconfig/network-scripts/ifcfg-eth0 /et ...
- c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow
c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow fcgio.cpp:50: error ...
- SqlDataAdapter的方法之一Fill (DataSet dataset, String datatable)解释
一.SqlDataAdapter的方法之一Fill (DataSet dataset, String datatable)解释:根据datatable名填充Dataset.myda.Fill(ds, ...