Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with foreach loop in MVC 3 Razor View

Problem with foreach loop in MVC 3 Razor View

Scheduled Pinned Locked Moved ASP.NET
asp-netarchitecturehelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    solomon201
    wrote on last edited by
    #1

    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

    N V 2 Replies Last reply
    0
    • S solomon201

      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

      N Offline
      N Offline
      n podbielski
      wrote on last edited by
      #2

      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... >: |

      M 1 Reply Last reply
      0
      • S solomon201

        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

        V Offline
        V Offline
        vicent estevan
        wrote on last edited by
        #3

        why don't you try to do this??: @foreach(var inmate in Model) { @inmate.inmateid }

        1 Reply Last reply
        0
        • N n podbielski

          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... >: |

          M Offline
          M Offline
          Mayank_Gupta_
          wrote on last edited by
          #4

          That really is a very silly mistake

          N 1 Reply Last reply
          0
          • M Mayank_Gupta_

            That really is a very silly mistake

            N Offline
            N Offline
            n podbielski
            wrote on last edited by
            #5

            Yes it is. But is not mine. You should answer to the question not mine answer. Take care.

            No more Mister Nice Guy... >: |

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups