Reputation: 248
i am trying to pass string of data from controller to view using viewbag but not able to fetch the value
in view side i have received the string of array values in viewbag at controller side and want to fill column of table in view side
controller
object o = command.Parameters["var_value"].Value;
if (o != null)
{
string val = o.ToString();
string[] values = val.Split('~');
ViewBag.values = values;
}
View
var [email protected] as string[];
<table class="table">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<tr>
<td>values[1]</td>
<td>values[2]</td>
<td>values[3]</td>
</tr>
Expected:Values should be bind in column at view side Actual:values are not bind in column at view side
Upvotes: 0
Views: 436
Reputation: 9502
Put the variable in a C# code block.
@{
var values = ViewBag.values as string[];
}
<table class="table">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<tr>
<td>@values[0]</td>
<td>@values[1]</td>
<td>@values[2]</td>
</tr>
Upvotes: 1
Reputation: 813
use it like this::
@foreach(var str in ViewBag.Collection)
{
@Html.Raw(str); <br/>
}
@for (int i = 0; i <= 2; i++ )
{
@Html.Raw(ViewBag.Collection[i]) <br/>
}
check out this link: Access a Viewbag like an Array?
Upvotes: 0