View从Action中获得数据和html helper function(转载)
2020-11-18 12:20
标签:com blog style class div code c log t sp width UIHelper.cshtml UIFunctions.cshtml controller View从Action中获得数据和html helper function(转载),搜素材,soscw.com View从Action中获得数据和html helper function(转载) 标签:com blog style class div code c log t sp width 原文地址:http://www.cnblogs.com/yhf286/p/3701176.html
@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;
}
GetData
这是从ViewData.Model中取出的数据 @ViewData.Model.Name
这是从ViewData[
"data"
]中取出的数据 @p.Age
这是从ViewBag.Data中取出的数据 @p2.Name @p2.Age
@{
int
i = 1;}
@*@helper ChangeColor(
int
age)
{
if
(age > 90)
{
"red"
>@age
}
else
{
@age
}
}*@
@*@functions{
public
IHtmlString ChangeColor(
int
age)
{
if
(age>90)
{
return
new
HtmlString(
""
+age+
""
);
}
else
{
return
new
HtmlString(age +
""
);
}
}
}*@
"1px"
cellpadding=
"2px"
cellspacing=
"0px"
width=
"500px"
style=
"border-collapse: collapse"
>
"20px"
>
ID
和尚
"50px"
>
年龄
"100px"
>
操作
@
foreach
(M_Person person
in
ViewBag.Persons)
{
"center"
>@(i++)
"center"
>@person.Name
@*
"center"
>@ChangeColor(person.Age)*@
@*
"center"
>@UIHelper.ChangeColor(person.Age)*@
@*
*@"center"
>@ChangeColor(person.Age)
"center"
>@UIFunctions.ChangeColor(person.Age)
"center"
>
删除||编辑
}
@helper ChangeColor(
int
age)
{
if
(age>90)
{
"red"
>@age
}
else
{
@age
}
}
@functions{
public
static
IHtmlString ChangeColor(
int
age)
{
if
(age > 90)
{
return
new
HtmlString(
""
+ age +
""
);
}
else
{
return
new
HtmlString(age +
""
);
}
}
}
public
ActionResult GetData()
{
M_Person person =
new
M_Person() { Name =
"济公活佛"
, Age = 90 };
ViewData[
"data"
] = person;
ViewData.Model = person;
ViewBag.Data = person;
List
new
List
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();
}
下一篇:JS and DOM 对象列表
文章标题:View从Action中获得数据和html helper function(转载)
文章链接:http://soscw.com/index.php/essay/21850.html