View从Action中获得数据和html helper function(转载)

2020-11-18 12:20

阅读:682

标签:com   blog   style   class   div   code   c   log   t   sp   width   

?
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;
}

    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">
    
        
        
        
        
    
    @foreach (M_Person person in ViewBag.Persons)
    {
 
        
            
            
           @*
*@
         
            
        
 
    }
"20px">
            ID
        
            和尚
        
"50px">
            年龄
        
"100px">
            操作
        
"center">@(i++)
            
"center">@person.Name
            
"center">@ChangeColor(person.Age)*@
          @*
"center">@UIHelper.ChangeColor(person.Age)*@
         @*
"center">@ChangeColor(person.Age) "center">@UIFunctions.ChangeColor(person.Age) "center">
                删除||编辑
            

  UIHelper.cshtml

?
1
2
3
4
5
6
7
8
9
10
11
12
@helper ChangeColor(int age)
{
 
if(age>90)
    {
"red">@age
    }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("" + age + "");
 
        }
        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 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();
  }

  

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


评论


亲,登录后才可以留言!