1

我想填充一个 IEnumerable 来构建一个循环,以按数组长度贯穿我。该值将被添加到函数中发生 2 次的结果列表中,即 Seaons 和 Categories。每个数组的长度为两个值。如果我现在花在 .cshtml 中,当两个值之一较高时,我会得到两次

        // Set List
        List<Template> result = new List<Template>();
        List<TemplateList> tmp_list = new List<TemplateList>();

        // Categories
        string[] Categories_Array = TemplateModel.GetRoomCategories.Split(',');
        // SeasonData
        string[] SeasonData_Array = TemplateModel.GetSeasonsData.Split(',');

        for (int i = 0; i < Categories_Array.Length; i++)
        {
            string[] Categories = Categories_Array[i].Split(';');                
            tmp_list.Add(new TemplateList { CategoreList = Categories[0], PriceList = "125,00" });

            for (int a = 0; a < SeasonData_Array.Length; a++)
            {
                string[] SeasonData = SeasonData_Array[a].Split(';');
                result.Add(new Template { GetSeasonsData = SeasonData[0] });
            }
        }

        result.Add(new Template { TemplateLists = tmp_list });

        return result.AsEnumerable();

cshtml

<table class="table" cellpadding="0" cellspacing="0" border="0">
<tr>
    <th class="header">Season</th>
    @foreach (var item in Model)
    { 
        <th class="header">@item.GetSeasonsData</th>
    }
</tr>
<tr>
    <td>
        <table cellpadding="0" cellspacing="0" border="0">
            @foreach (var item in Model)
            { 
                foreach (var extra in item.TemplateLists)
                {
                    <tr class="double">
                        <td class="roomlist">@extra.CategoreList</td>
                        <td class="pricelist">@extra.PriceList</td>
                    </tr>
                }
            }
        </table>
    </td>
</tr>

阵列:类别EZ;1,DZ;2 季节Summer;01.12.2013;31.12.2013,Winter;01.06.2013;31.06.2013

仅当有 3 个类别时,只有 1 个季节仍然可用 3 个“tr”,没有按季节加载

谢谢你

4

1 回答 1

1

您有效地创建了两次模板,一组仅包含密码,另一组仅包含用户名。您也没有User_Array在第二个循环中循环。如果您想List<Template>使用用户名和密码创建一个,请尝试以下操作:

List<Template> result = new List<Template>();

// Password
string[] Password_Array = TemplateModel.Password.Split(',');
// User
string[] User_Array = TemplateModel.User.Split(',');

for (int i = 0; i < User_Array.Length; i++)
{
    string[] users = User_Array[i].Split(';');

    // stop it blowing up if there are fewer passwords than usernames 
    // given that we are indexing against the username array
    string password = string.Empty;
    if (Password_Array.Length > i) {
        string[] passwords = Password_Array[i].Split(';');
        password = passwords[0];
    }

    result.Add(new Template { User = users[0], Password = password });
}

旁注:您可能希望查看使用Dictionary<string, string>用户名作为键的 a 而不是 2 个数组。

于 2013-09-19T10:35:37.117 回答