【问题标题】:How to Bind IDictionary to Repeater different controls?如何将 IDictionary 绑定到 Repeater 不同的控件?
【发布时间】:2020-12-17 01:33:45
【问题描述】:

我有一个IDictionary 对象:

IDictionary<int, string> keyPair = new Dictionary<int, string>();
keyPair.Add(1, "value1");
keyPair.Add(2, "value2");
keyPair.Add(3, "value3");
keyPair.Add(4, "value4");

我有 HTML 代码:

<table>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>4</td>
  </tr>
</table>

我想在 td1 中的 key=1 和 td2 中的 key=2 处绑定值,等等

【问题讨论】:

标签: c# asp.net model-view-controller repeater


【解决方案1】:

-首先:在您的控制器中,确保将您的字典添加到视图包中:

            ViewBag.keyPair = keyPair;

-然后在你的cshtml页面中添加:

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

【讨论】:

    猜你喜欢
    • 2013-11-02
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多