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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:29:32+00:00 2026-05-25T14:29:32+00:00

I have a model that can have a variable amount of items in a

  • 0

I have a model that can have a variable amount of items in a List<T>

In my view I then have the following:

@using (Html.BeginForm())
{
    int count = Model.Data.Filters.Count;
    for(int i = 0; i < count; i++)
    {
        <div>

        @Html.TextBox("filtervalue" + i)
        @Html.DropDownList("filteroptions"+i,Model.Data.Filters[i].FilterOptions)


        </div>
    }
    @Html.Hidden("LinkID", Url.RequestContext.RouteData.Values["id"])
}

Is there a way in my controller so I can set up the POST action method to bind to a model with variable items in it?

Also how would I construct the model to cope with this?

Thanks

  • 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-25T14:29:32+00:00Added an answer on May 25, 2026 at 2:29 pm

    You coud use editor templates, it will be much easier:

    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.Data.Filters)
        @Html.Hidden("LinkID", Url.RequestContext.RouteData.Values["id"])
    }
    

    and inside the editor template (~/View/Shared/EditorTemplates/FilterModel.cshtml) which will be automatically rendered for each element of the Model.Data.Filters collection:

    @model FilterModel
    <div>
        @Html.TextBoxFor(x => x.FilterValue)
        @Html.DropDownListFor(x => x.SelectedFilterOption, Model.FilterOptions)
    </div>
    

    Now your POST controller action will simply look like this:

    [HttpPost]
    public ActionResult Foo(SomeViewModel model)
    {
        // model.Data.Filters will be properly bound here
        ...
    }
    

    Thanks to editor templates you no longer have to write any foreach loops in your views or worry about how to name your input fields, invent some indexed, … so that the default model binder recognizes them on postback.

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

Sidebar

Related Questions

In my controller I have model operations that can return empty results. I've setup
Does jquery/javascript have any event model that you can attach to your objects? Basically
How can I override the IsValid property? I have a model that's validating as
I want to have a model with calculated fields that I can apply sorting
I have a View that can vary significantly, depending on the 'mode' a particular
I'm using the Play! framework and I have a model (an Entity) that has
I have a user model that is generated using Devise. I am extending this
The documentation implies that you can either have 3D rendering or framebuffer mode, and
Say that I have two models- Users and Accounts. Each account can have at
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)

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.