MaximusPrime MaximusPrime - 3 months ago 13
C# Question

Best practice for using for loop in an mvc view

First: Sorry for the lengthy post I just wanted to make sure all the details were there

I have the following for loop:

for (var n = 0; n <= person.Phones.Count - 1; n++)
{
var phone = new PhoneItemViewModel
{
PhoneId = person.Phones[n].Phone_ID,
PhoneTypeId = person.Phones[n].PhoneType_ID,
PhoneTypeDesc = person.Phones[n].PhoneTypeDesc,
PhoneNumber = person.Phones[n].PhoneNumber,
Extension = person.Phones[n].Extension,
AllowAccountingText = person.Phones[n].AllowAccountingText,
DoNotCall = person.Phones[n].DoNotCall
};
vm.Phones.Add(phone);



}


What I need to do is take each of the values like :

PhoneNumber = person.Phones[n].PhoneNumber


and put that value into the view model value which looks like this

vm.HPhone1 = ????? --This is where I am unclear of what to use


I do know the array values for each model value using the example above the array value would be (0)

So I know something like this should work

vm.HPhone1 = person.Phones[0].PhoneNumber


and that would if there were only one value but at times there are 2, 3, 4 etc values that use the "PhoneNumber" and if I were to put all of them like the above example then it would error out with an "index out of range" error.

Sample code:

for (var n = 0; n <= person.Phones.Count - 1; n++)
{
var phone = new PhoneItemViewModel
{
PhoneId = person.Phones[n].Phone_ID,
PhoneTypeId = person.Phones[n].PhoneType_ID,
PhoneTypeDesc = person.Phones[n].PhoneTypeDesc,
PhoneNumber = person.Phones[n].PhoneNumber,
Extension = person.Phones[n].Extension,
AllowAccountingText = person.Phones[n].AllowAccountingText,
DoNotCall = person.Phones[n].DoNotCall
};
vm.Phones.Add(phone);

vm.HPhone1 = person.Phones[0].PhoneNumber; <--Multiples of these with different array values
vm.HPhone2 = person.Phones[5].PhoneNumber;
vm.BPhone1 = person.Phones[1].PhoneNumber;
vm.BExtension1 = person.Phones[1].Extension;
vm.BPhone2 = person.Phones[6].PhoneNumber;
vm.BExtension2 = person.Phones[6].Extension;
vm.CPhone = person.Phones[2].PhoneNumber;
vm.Pager = person.Phones[3].PhoneNumber;
vm.Fax1 = person.Phones[4].PhoneNumber;
vm.Fax2 = person.Phones[7].PhoneNumber;
vm.Other1 = person.Phones[8].PhoneNumber;



}


The end result of each of these is a string in each object they all will be phone numbers in string format (1234567890) no dashes or formatting. It iterates through an object and assigns the values of the array to each of the fields. So each array could or could not have a value for vm.HomePhone1

Answer

You can use foreach for this: https://msdn.microsoft.com/nl-nl/library/ttw7t8t6.aspx

foreach (var phone in person.Phones) 
{
    vm.Phones.Add(new PhoneItemViewModel
                        {
                            PhoneId = phone.Phone_ID,
                            PhoneTypeId = phone.PhoneType_ID,
                            PhoneTypeDesc = phone.PhoneTypeDesc,
                            PhoneNumber = phone.PhoneNumber,
                            Extension = phone.Extension,
                            AllowAccountingText = phone.AllowAccountingText,
                            DoNotCall = phone.DoNotCall
                        };
}