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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:51:04+00:00 2026-05-21T07:51:04+00:00

So I asked a question last year about having an add item button that’ll

  • 0

So I asked a question last year about having an “add item” button that’ll generate a new row in a form in JavaScript that I can return to an MVC POST action handler.

For instance, if I have a form generated like this:

@for(int i = Model.Notes.Count - 1; i >= 0; i--)
{
    <tr>
        <td>@Html.HiddenFor(m => m.Notes[i].Id, new Dictionary<string, object> { { "data-index", i } } )
        @Html.TextBoxFor(m => m.Notes[i].Timestamp, new { @class = "datepicker" })</td>
        <td>@Html.DropDownListFor(m => m.Notes[i].PersonId, new SelectList(Model.People, "Id", "Name"))</td>
        <td>@Html.EditorFor(m => m.Notes[i].Amount)</td>
        <td>@Html.EditorFor(m => m.Notes[i].Comment)</td>
    </tr>
}

The HTML it generates for a single row:

<tr>
                <td><input data-index="2" data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Notes_2__Id" name="Notes[2].Id" type="hidden" value="16192">
                <input class="datepicker hasDatepicker valid" data-val="true" data-val-required="The Timestamp field is required." id="Notes_2__Timestamp" name="Notes[2].Timestamp" type="text" value="11/04/2011 2:11:21 PM"></td>
                <td><select id="Notes_2__PersonId" name="Notes[2].PersonId">
<option value="8367">Rei</option>
<option value="8668">Ray</option>
<option value="8883">Roy</option>
<option value="8814">Rob</option>
<option value="8886">Ron</option>
</select></td>
                <td><input class="text-box single-line" data-val="true" data-val-number="The field Amount must be a number." data-val-required="The Amount field is required." id="Notes_2__Amount" name="Notes[2].Amount" type="text" value="0.00"></td>
                <td><input class="text-box single-line" id="Notes_2__Comment" name="Notes[2].Comment" type="text" value="Some comment"></td>
            </tr>

If I want to add an item in MVC2, I had to write some really dry JavaScript code to determine the index of the latest item, generate all the trs, tds, and input HTML and set the attributes to match.

Not only is this really non-trivial, if I ever change something in the cshtml, I have to be very careful to make sure all the JavaScript matches. It’s a really nasty duplication of code.

Is there any better way to do it in MVC3?

Thanks in advance,

Rei

  • 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-21T07:51:05+00:00Added an answer on May 21, 2026 at 7:51 am

    Here’s a blog post illustrating that the situation could be better even in MVC 2 🙂 And the situation could be even further improved by using editor templates (which also exist in MVC 2) and never write a single for loop in a view.

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

Sidebar

Related Questions

I asked a question yesterday about a problem that Im having with a program
This is a follow-up to a question I asked last year about setting up
I asked a question last week about how to loop a YouTube video from
I asked a similar question last week but did not get an answer that
In my last question I asked about parsing the links out of an HTML
I've asked a previous question last month, about whether an Entity should access a
Last week user Masse asked a question about recursively listing files in a directory
The last question I asked concerned how to bin data by x co-ordinate. The
I was asked a question in C last night and I did not know
I guess this is a continuation of the last question I asked: bulk insert

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.