1

I have to print/display the values of few variables in a MVC view. How can I do this..

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"
    }
2
  • Send off a Dictionary<String,Object> to the view and iterate over it? Or possibly use reflection and automate the process (assuming you're just stripping properties off the ViewBag) Commented Dec 19, 2011 at 14:34
  • and can you show your view definition? Commented Dec 19, 2011 at 14:35

4 Answers 4

4

You can do this in the view by doing something like

@ViewBag.Message.var1

It would be better to create a view model though and pass that to your view.

Sign up to request clarification or add additional context in comments.

1 Comment

please see the solution I came up with.. . how does that look?
0

I used expando object to do this. Please see my response below..

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"
}

and in the View.. I have

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

How is this solution.. seems to work for me.

Comments

0

For completeness, here's a very basic example of how you'd create a strongly typed view, a viewmodel, and pass data.

Define a view model:

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

Populate it in your controller:

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

    return View(model);
}

Finally, declare the model in your view (in 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 />

Comments

-1

Well, it's a bit old post now but it could save time for others. I done a Partial View which use reflection to display objects properties

@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>

Then i use this as

@{
    Layout = null;
}

<!DOCTYPE html>

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

Do not forget to set the ViewBag.ObjectToDisplay in your controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.