Click here to Skip to main content
       

ASP.NET

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionProblem with foreach loop in MVC 3 Razor Viewmembersolomon20114 Nov '12 - 4:09 
I have a loop in my razor view as
below
@foreach(var inmate in Model)
{
@inmate.inmateid
@inmate.inmateid
}
My Model is an IEnumerable of inmates
containing 2 inmate objects. But the
result I am getting in my browser is
6
6
7
7
Instead of
6
7
Please can someone tell me why this is
the case?
Thanks
AnswerRe: Problem with foreach loop in MVC 3 Razor Viewmembern.podbielski14 Nov '12 - 6:34 
Because you enumerating collection, and for each item You are writing this item twice. If you want it just once remove one @inmate.inmateid from inside foreach loop.
No more Mister Nice Guy... >: |

GeneralRe: Problem with foreach loop in MVC 3 Razor Viewmembermayankgupta68828 Nov '12 - 15:28 
That really is a very silly mistake
GeneralRe: Problem with foreach loop in MVC 3 Razor Viewmembern.podbielski29 Nov '12 - 6:22 
Yes it is. But is not mine. You should answer to the question not mine answer.
Take care.
No more Mister Nice Guy... >: |

AnswerRe: Problem with foreach loop in MVC 3 Razor Viewmembervicent estevan19 Nov '12 - 2:11 
why don't you try to do this??:
 
@foreach(var inmate in Model)
{
@inmate.inmateid
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid