2011-12-19 83 views
1

我必須在MVC視圖中打印/顯示少量變量的值。我怎樣才能做到這一點..在MVC視圖中顯示變量列表

public ActionResult DisplayVariables() 
    { 
     ViewBag.Message = "Welcome to ASP.NET MVC!";   
     {    
      //After some process/logic.. variables get assigned values here.. 
       int var1 = xxxxxx; 
       int var2 = xxxxx; 
       int var3 = xxxxxx; 
       int var4 = xxxxx;  
     } 
     return View() //I want to display variables values with lables here in this 
         //view. I already have a view with name "DisplayVariables" 
    } 
+0

發送一個'Dictionary '到視圖並迭代它?或者可能使用反射和自動化過程(假設你只是從'ViewBag'剝離屬性) – 2011-12-19 14:34:52

+0

,你可以顯示你的視圖定義? – 2011-12-19 14:35:03

+0

請看下面我的回答..謝謝 – ZVenue 2011-12-19 14:54:10

回答

0

我用expando對象來做到這一點。請參考下面我的回答..

public ActionResult DisplayVariables() 
{ 
    ViewBag.Message = "Welcome to ASP.NET MVC!";  

    dynamic Model = new ExpandoObject(); 

    {    
     //After some process/logic.. variables get assigned values here.. 

      int var1 = xxxxxx; 
      int var2 = xxxxx; 
      int var3 = xxxxxx; 
      int var4 = xxxxx; 

      Model.var1 = var1; 
      Model.var2 - var2; 
      Model.var3 = var3; 
      Model.var4 - var4; 
    } 

    return View(Model) //I want to display variables values with lables here in this 
        //view. I already have a view with name "DisplayVariables" 
} 

,並在視圖..我有

<h2><%=Model.var1%></h2> 

這是怎麼解決..似乎爲我工作。

4

您可以在視圖中通過執行類似

@ViewBag.Message.var1 

這將是更好的,雖然創建一個視圖模型,並傳遞到您的視圖做到這一點。

+0

請看我想出的解決方案..這看起來如何? – ZVenue 2011-12-19 14:53:28

0

爲了完整起見,下面是如何創建強類型視圖,視圖模型和傳遞數據的非常基本的示例。

定義視圖模型:

namespace MyNameSpace.ViewModels 
{ 
    public class MyViewModel 
    { 
     public string Var1; 
     public string Var2; 
     public string Var3; 
    } 
} 

填充它在你的控制器:

public ActionResult DisplayVariables() 
{ 
    var model = new MyViewModel 
    { 
     Var1 = "Foo", 
     Var2 = "Bar", 
     // ... 
    } 

    return View(model); 
} 

最後,在你看來聲明模型(DisplayVariables.cshtml)

@* Note the small 'm' here. You'll probably need to 
    put a namespace in front of the type name as well. *@ 
@model MyNameSpace.ViewModels.MyViewModel 

@Model.Var1<br /> 
@Model.Var2<br /> 
-1

那麼,現在這個帖子有點老了,但它可以爲其他人節省時間。 我做了局部視圖,其使用反射來顯示對象的屬性

@using System.Reflection; 
@{ 
    Object l_o = ViewBag.ObjectToDisplay; 
} 
<style type="text/css"> 
    table,th,td 
    { 
     border:1px solid green; 
     border-collapse:collapse; 
     padding:3px; 
    } 
    th 
    { 
     background-color:green; 
     color:white; 
    } 
    #display-title 
    { 
     font-size:large; 
     color:green; 
    } 
</style> 
<div> 
    <div id="display-title">@l_o.GetType().Name</div> 
    <table> 
    <tr> 
     <th>Property</th> 
     <th>Value</th> 
    </tr> 

    @foreach (PropertyInfo l_info in l_o.GetType().GetProperties()) 
    { 
     <tr> 
      <td> 
       <div class="display-label" style="text-align: right;"> 
        @l_info.Name 
       </div> 
      </td> 
      <td> 
       <div class="display-value"> 
        @l_info.GetValue(l_o, null) 
       </div> 
      </td> 
     </tr> 
    } 
    </table> 
</div> 

然後我用這個作爲

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <title>Register</title> 
</head> 
<body> 
    @Html.Partial("ObjectPropertiesView") 
</body> 
</html> 

不要忘記設置ViewBag.ObjectToDisplay在您的控制器。