【问题标题】:Apply c# dictionary output to html table [closed]将c#字典输出应用到html表[关闭]
【发布时间】:2023-03-30 03:36:01
【问题描述】:

我有一个包含字符串和整数的字典,如果可能的话,我想将字典字符串和整数解析为 html 表?

protected void Page_Load(object sender, EventArgs e)
    {

        Dictionary<string, int> userMediaList = new Dictionary<string, int>();
        userMediaList.Add("Die Hard", 5);
        userMediaList.Add("Die Hard II", 3);
        userMediaList.Add("Die Hard III", 2);
        userMediaList.Add("Ringenes Herre", 4);
        userMediaList.Add("Ringenes Herre II", 1);
        userMediaList.Add("Ringenes Herre III", 5);

        var sortUserMediaList = from entry in userMediaList orderby entry.Value ascending select entry;

    }

所以我想要将其添加到网站上的表格中。

这样对于表格中的每个框,都会显示电影的名称和电影的评分。

【问题讨论】:

  • 当然可以。这取决于您使用的技术...
  • Geez Andrei V. 仇恨怎么了.... @Winkz,您可能想看看 stackoverflow.com/questions/2274875/bind-dictionary-to-repeater ,他们将字典绑定到中继器。这可以很容易地做你想要的。
  • 它在网络应用程序中吗?
  • 好吧,我猜这很好:D 很好,我正在使用 foreach 循环遍历条目。只是不知道如何将每一行应用到表中的新“框”。
  • 谢谢你,戴夫,我会试着读一读! Ufuk 这是一个网页,所以我想是的 :)

标签: c# html dictionary code-behind


【解决方案1】:

由于您没有说明您使用的是什么技术,因此这是一种通用方式:

var trs = dictionary.Select(a => String.Format("<tr><td>{0}</td><td>{1}</td></tr>", a.Key, a.Value));

var tableContents = String.Concat(trs);

var table = "<table>" + tableContents + "</table>";

【讨论】:

  • 将其添加到代码中没有任何结果:/我需要在 html 部分添加一些内容吗?
  • 你需要以某种方式渲染它。恐怕你没有真正提供足够的信息让人们正确地帮助你。
  • 我想再次编辑主帖:p
【解决方案2】:

你可以从那开始,但可能有更好的方法:

string html = "<table>";
    foreach (KeyValuePair<string,int> pair in userMediaList)
    {
        html = html + String.Format("<tr><td>{0}</td><td>{1}</td></tr>", pair.Key, pair.Value.ToString());
    }
html = html + "</table>";

【讨论】:

  • 将其添加到代码中没有任何结果:/我需要在 html 部分添加一些内容吗?
猜你喜欢
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多