I have an IDictionary object:
IDictionary<int, string> keyPair = new Dictionary<int, string>();
keyPair.Add(1, "value1");
keyPair.Add(2, "value2");
keyPair.Add(3, "value3");
keyPair.Add(4, "value4");
And I have HTML code:
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
I want to bind value where key=1 inside td1 and key=2 inside td2 and so
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
-First :In you Controller make sure to add your dictionary in a viewbag :
ViewBag.keyPair = keyPair;
-Then in your cshtml page add :
<table>
<tr>
<td>@ViewBag.keyPair[1]</td>
<td>@ViewBag.keyPair[2]</td>
</tr>
<tr>
<td>@ViewBag.keyPair[3]</td>
<td>@ViewBag.keyPair[4]</td>
</tr>
</table>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0