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 3988834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:17:59+00:00 2026-05-20T06:17:59+00:00

So I am new to ASP.NET MVC and I would like to create a

  • 0

So I am new to ASP.NET MVC and I would like to create a view with a text box for each item in a collection. How do I do this, and how do I capture the information when it POSTs back? I have used forms and form elements to build static forms for a model, but never dynamically generated form elements based on a variable size collection.

I want to do something like this in mvc 3:

@foreach (Guest guest in Model.Guests)
{
    <div>
        First Name:<br />
        @Html.TextBoxFor(???) @* I can't do x => x.FirstName here because
                                 the model is of custom type Invite, and the
                                 lambda wants to expose properties for that
                                 type, and not the Guest in the foreach loop. *@
    </div>
}

How do I do a text box for each guest? And how do I capture them in the action method that it posts back to?

Thanks for any help.

  • 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-20T06:17:59+00:00Added an answer on May 20, 2026 at 6:17 am

    Definitely a job for an editor template. So in your view you put this single line:

    @Html.EditorFor(x => x.Guests)
    

    and inside the corresponding editor template (~/Views/Shared/EditorTemplates/Guest.cshtml)

    @model AppName.Models.Guest
    <div>
        First Name:<br />
        @Html.TextBoxFor(x => x.FirstName)
    </div>
    

    And that’s about all.

    Now the following actions will work out of the box:

    public ActionResult Index(int id)
    {
        SomeViewModel model = ...
        return View(model);
    }
    
    [HttpPost]
    public ActionResult Index(SomeViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        // TODO: do something with the model your got from the view
        return RedirectToAction("Success");
    }
    

    Note that the name of the editor template is important. If the property in your view model is:

    public IEnumerable<Guest> Guests { get; set; }
    

    the editor template should be called Guest.cshtml. It will automatically be invoked for each element of the Guests collection and it will take care of properly generating ids and names of your inputs so that when you POST back everything works automatically.

    Conclusion: everytime you write a loop (for or foreach) inside an ASP.NET MVC view you should know that you are doing it wrong and that there is a better way.

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

Sidebar

Related Questions

I am trying to start a new project (asp.net MVC) and I would like
First of all, I'm really new to the MVC Asp.Net ideology. I would like
I created a new ASP.NET MVC application. The home page looks like this: I
this is the case: ASP.NET MVC C# Model: I would like to use information
I'm trying to learn the new ASP .NET MVC framework and would like to
In my asp.net MVC 3 project I would like to create a contact that's
I would like to implement a ASP.NET MVC View Engine to perform some pre-processing
Some background: I create a new ASP.NET MVC 3 WebApplication. Then I add a
I am new to ASP.NET MVC. I am trying to create what I thought
I have built a little repeater HtmlHelper for asp.net MVC and I would like

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.