显示MVC视图中的变量列表

时间:2011-12-19 14:30:47

标签: asp.net-mvc asp.net-mvc-3 view

我必须在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"
    }

4 个答案:

答案 0 :(得分:4)

您可以通过执行类似

的操作在视图中执行此操作
@ViewBag.Message.var1

最好创建一个视图模型并将其传递给您的视图。

答案 1 :(得分: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> 

这个解决方案怎么对我有用。

答案 2 :(得分: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 />

答案 3 :(得分:-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。