C# 将c字典输出应用于html表
我有一个带有string和int的字典,如果可能的话,我想将字典字符串和int解析为一个html表C# 将c字典输出应用于html表,c#,html,dictionary,code-behind,C#,Html,Dictionary,Code Behind,我有一个带有string和int的字典,如果可能的话,我想将字典字符串和int解析为一个html表 protected void Page_Load(object sender, EventArgs e) { Dictionary<string, int> userMediaList = new Dictionary<string, int>(); userMediaList.Add("Die Hard", 5);
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;
}
所以我想把它添加到网站上的一个表中
因此,对于表中的每个框,都会显示电影名称和电影评级。您可以从这开始,但可能有更好的方法:
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>";
由于您没有说明正在使用的技术,因此这是一种通用方法:
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>";
当然有可能。这取决于你使用的技术……天哪,安德烈五世。仇恨是怎么回事@Winkz,你可能想看看,他们把一个发音符号绑在一个复述器上。这可以很容易地做到你想要的。它是在一个web应用程序中吗?好的,那很好,我想:D well atm im使用foreach循环遍历条目。。只是不知道如何将每一行应用到表中的新框中。谢谢你,戴夫,我要试着读一下!Ufuk它是一个网页,所以我想:将此添加到代码中不会出现任何问题:/i需要在html部分添加一些内容吗?将此添加到代码中不会出现任何问题:/i需要在html部分添加一些内容吗?您需要以某种方式呈现它。恐怕你没有给人们提供足够的信息来帮助你。我想你会再编辑一次主帖吗