Reputation: 5027
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"
}
Upvotes: 1
Views: 6735
Reputation:
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.
Upvotes: -1
Reputation: 5024
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 />
Upvotes: 0
Reputation: 5027
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.
Upvotes: 0
Reputation: 46750
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.
Upvotes: 4