Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7093571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:27:21+00:00 2026-05-28T08:27:21+00:00

<ul> @{int i=0;} @foreach (var entry in Model.PhoneNumberEntries) { <li> <span>@entry.PhoneNumber<span> @Html.TextBoxFor(m=>Model.PhoneNumberEntries[i].PhoneNumber)</li> i++; }

  • 0
    <ul>
    @{int i=0;}
    @foreach (var entry in Model.PhoneNumberEntries)
     {
         <li>
             <span>@entry.PhoneNumber<span> 
             @Html.TextBoxFor(m=>Model.PhoneNumberEntries[i].PhoneNumber)</li>
         i++;
     }
     </ul>

This seems a little too verbose for me… is there way to get around creating the counter with having to resort to the standard for loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:27:22+00:00Added an answer on May 28, 2026 at 8:27 am

    This seems a little too verbose for me… is there way to get around
    creating the counter with having to resort to the standard for loop?

    Yeah it seems too verbose to me as well. Even the loop seems too verbose as you don’t need it if you use editor templates.

    <ul>
        @Html.EditorFor(x => x.PhoneNumberEntries)
    </ul>
    

    and then obviously you would define a custom editor template that will automatically be rendered for each element of the PhoneNumberEntries collection (~/Views/Shared/EditorTemplates/PhoneNumberEntry.cshtml):

    @model PhoneNumberEntry
    <li>
        <span>@Model.PhoneNumber</span> 
        @Html.EditorFor(m => m.PhoneNumber)
    </li>
    

    You don’t even need to write loops as templates work by convention.

    Notice that the name and the location of the editor template is important. It should be located either inside ~/Views/Shared/EditorTemplates if you want to share this template between views belonging to different controllers in your application and it is the location where ASP.NET MVC will first look for it. Or you could also put it inside ~/Views/XXX/EditorTemplates where XXX is the name of the current controller. Then name of the editor template must be the name of the type used as agrument for the collection property.

    So if you had no your main view model:

    public IEnumerable<FooBarViewModel> FooBars { get; set; }
    

    the name of the corresponding template would be FooBarViewModel.cshtml and obviously it will be strongly typed to FooBarViewModel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this method: public IEnumerable<T> GetList(int Count) { foreach (var X in Y) {
@{int i=0;} @foreach (var e in Model.BlockList) { @foreach (var e2 in e.RadioButtons) {
This is the Code I'm having problems with: @{int i = 1;} @foreach (var
So I have a razor view: @{ int i=1; foreach(var story in Model.Storylines) {
Behold the C# code: IEnumerable<int> innerMethod(int parameter) { foreach(var i in Enumerable.Range(0, parameter)) {
foreach (int i in a.set) { foreach (int k in b.set) { if (i
When i have a code block static void Main() { foreach (int i in
int a[2]; This in memory actually looks like: //Assuming int is 2 bytes add=2000,
I have a class named Entry declared like this: class Entry{ string Id {get;set;}
Here is a simple scenario in C#: var intList = new List<int>(); intList.Add(4); intList.Add(7);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.