View从Action中获得数据和html helper function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@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

1
2
3
4
5
6
7
8
9
10
11
12
@helper ChangeColor(int age)
{
 
if(age>90)
    {
<font color="red">@age</font>
    }else
    {
@age
 
    }   
}

  UIFunctions.cshtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@functions{
 
    public static IHtmlString ChangeColor(int age)
    {
 
        if (age > 90)
        {
 
            return new HtmlString("<font color=‘red‘>" + age + "</font>");
 
        }
        else
        {
 
            return new HtmlString(age + "");
 
        }
 
    }
 
}

  controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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(转载),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。