如何在razor html中使用foreach循环显示字符串数组 [英] How to display array of strings using foreach loop in razor html
问题描述
大家好,
我是mvc的新手,我可能面临一些愚蠢的问题,但我需要一些帮助来解决这个简单的问题..
我的问题是例如我有两个字符串数组列表说
string [] products = new string [] {comp ,TV}
string []公司=新字符串[] {Google,Microsoft}
现在我有有两个表头的表格说产品和公司,我希望它们显示如下
产品公司
Comp GOogle
TV Microsoft
现在我需要同样地显示上面的动态,我知道我们可以使用foreach循环(var prod in products)
{
td prod td
}但如果我想要更新我应该使用另一个foreach与公司进行循环,所以请帮助解决这个问题与一个foreach所以更新两个阵列动态地在一个时间内输入字符串。
Hi all,
I am new to mvc and i have been facing some silly issue may be but i need some help to resolve this simple one..
My prob is for example i am having two list of strings arrays say
string[] products= new string[]{"comp","TV"}
string[] Companies=new string[]{"Google","Microsoft"}
Now i have tables with two table headers say products and companies and I want them to be displayed like below
Product Companies
Comp GOogle
TV Microsoft
Now i need to display like above simulataneously that too dynamically i know we can use foreach loop with (var prod in products)
{
td prod td
}but if i want to update i should use another foreach with companies for looping so please help to solve this issue with a single foreach so to update both arrays of strings in a single time dynamically.
推荐答案
现在我需要同样地显示上面的动态,我知道我们可以使用foreach循环(产品中的var prod)
Now i need to display like above simulataneously that too dynamically i know we can use foreach loop with (var prod in products)
完成 foreach
循环以处理1个数组/列表/集合。为了同时处理2个列表,对于循环使用简单的更容易。
The foreach
loop is done to handle 1 array/list/collection. In order to handle 2 lists simultaneously, it is easier to use a simple for
loop.
你好:
我认为以下内容将起作用
Hello:
I think that the following will work
<br />
@{<br />
int i=0;<br />
foreach (var prod in products) {<br />
var comp=Companies[i];<br />
... // use here <var>prod</var> and comp to <var>comp</var> as you need<br />
i++;<br />
}<br />
}@<br />
这篇关于如何在razor html中使用foreach循环显示字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!