0

I am looking for a proper syntax. I am using MVC and in my view I need to loop through the model since I need to do some other things at different counts. I am a novice here and looked through tons of postings but didn't find the syntax. Here is the code, which works:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcMysurvey.Models.Mysurvey>>" %>
<% foreach (var item in Model) {%>
            <%: Html.HiddenFor(m => item.ID) %>

And here is the code I need the syntax for to make it working:

   <% for (int i = 0; i < Model.Count(); i++) {%>
             <%: Html.HiddenFor(m => m[i].ID) %>
3
  • What is wrong with using foreach? Commented Jun 14, 2012 at 20:07
  • Is it just the "loop count" that you're missing ? Is that why you want to have for loop instead ? By the way, the For loop syntax seems ok to me. What's the problem ? Commented Jun 14, 2012 at 20:10
  • Why is it that a foreach isn't what you need? the two are very similar Commented Jun 15, 2012 at 0:36

2 Answers 2

5

Your model is of type IEnumerable<MvcMysurvey.Models.Mysurvey>.

The problem is that IEnumerable<T> does not expose an indexer ([]) operator. You have two options for dealing with this inconvenience.

  1. Use an array or a list instead.
    Arrays and IList<T> implementations do expose an indexer operator. To use the indexer syntax, you need to convert your model to an array or other IList<T>. So, just change your

    <% for (int i = 0; i < Model.Count(); i++) {%>
       <%: Html.HiddenFor(m => m[i].ID) %>
    

    to

    <% var items = Model.ToArray();
       for (int i = 0; i < items.Length; i++) {%>
          <%: Html.HiddenFor(m => items[i].ID) %>
    
  2. Use the the ElementAt method method

    While IEnumerable<T> does not expose an indexer operator, there is a LINQ extension method that does the same thing. It is the ElementAt method. To use this syntax, you could change your code to:

     <% for (int i = 0; i < Model.Count(); i++) {%>
        <%: Html.HiddenFor(m => m.ElementAt(i).ID) %>
    

The array syntax will be almost certainly be more efficient though.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1456934, You can select this answer by clicking the checkbox to the left of my post.
0

I think this may be what you're looking for? Your question isn't too clear but it sounds like you're looking to do something based on a condition that the count is equal to something. If that's the case you only need to add an if statement to check the count against whatever value you're looking for.

<% for (int i = 0; i < Model.Count(); i++)
<% { %>
    <% if (i == someCount) %>
    <% { %>
        <!-- Do Something Here -->
    <% } %>
    <%: Html.HiddenFor(m => m[i].ID) %>
<% } %>

1 Comment

this did not work because of IENUMERABLE<T> as explained in the posting above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.