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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:24:50+00:00 2026-06-13T21:24:50+00:00

I have a partial view that has textfields in it and is bound to

  • 0

I have a partial view that has textfields in it and is bound to a List set up, so essentially there is a row of textfields that the user can add another row to or remove a row from. I build the view by looping through the list like this:

@model PricingRequestWebApp.Web.Models.PricingRequestModel

<div id="shiplocation-wrapper">
    <table class="shipinfoprtable">
        <thead>
            <tr>
                <th>@Html.LabelFor(m => m.ShippingLocationsModel[0].Address)<span class="formRed">*</span></th>
                <th>@Html.LabelFor(m => m.ShippingLocationsModel[0].City)<span class="formRed">*</span></th>
                <th>@Html.LabelFor(m => m.ShippingLocationsModel[0].State)<span class="formRed">*</span></th>
                <th>@Html.LabelFor(m => m.ShippingLocationsModel[0].Zip)<span class="formRed">*</span></th>
                <th>@Html.LabelFor(m => m.ShippingLocationsModel[0].PFreight)<span class="formRed">*</span></th>
                <th>@Html.LabelFor(m => m.ShippingLocationsModel[0].FreightType)<span class="formRed">*</span></th>
                <td><span class="link" id="addlocation" onclick="AddShippingLocation()">Add</span></td>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.ShippingLocationsModel.Count; i++)
            {
                <tr>
                    <td>@Html.TextBoxFor(m => m.ShippingLocationsModel[i].Address, new { @style = "width: 200px" })</td>
                    <td>@Html.TextBoxFor(m => m.ShippingLocationsModel[i].City, new { @style = "width: 150px" })</td>
                    <td>@Html.TextBoxFor(m => m.ShippingLocationsModel[i].State, new { @size = 3 })</td>
                    <td>@Html.TextBoxFor(m => m.ShippingLocationsModel[i].Zip, new { @style = "width: 80px" })</td>
                    <td>@Html.TextBoxFor(m => m.ShippingLocationsModel[i].PFreight, new { @style = "width: 80px" })</td>
                    <td>@Html.TextBoxFor(m => m.ShippingLocationsModel[i].FreightType, new { @style = "width: 200px" })</td>
                    @if (Model.ShippingLocationsModel.Count > 1)
                    {
                        <td><span class="link" id="removelocation" onclick="RemoveShippingLocation(@i);">Remove</span></td>
                    }
                </tr>
            }
        </tbody>
    </table>
</div>

My controller methods look like so:

    [HttpPost]
    public ActionResult AddShippingLocation(PricingRequestModel model)
    {
        model.ShippingLocationsModel.Add(new ShippingLocationsModel());    

        return PartialView("shiplocationsPartial", model);
    }

    [HttpPost]
    public ActionResult RemoveShippingLocation(PricingRequestModel model)
    {
        model.ShippingLocationsModel.RemoveAt(StaticItems.id);

        return PartialView("shiplocationsPartial", model);
    }

And my Jquery function that posts the data and returns the view:

function AddShippingLocation() {
    $.ajax({
        data: $('#shippinginfoform').serialize(),
        type: "POST",
        url: "/PricingRequest/AddShippingLocation",
        success: function (response) {
            $('#shiplocation-wrapper').html(response);
        }
    })
}

function RemoveShippingLocation(id) {
    $.ajax({
        data: { id: id },
        type: "POST",
        url: "/PricingRequest/SaveID",
        complete: function () {
            $.ajax({
                data: $('#shippinginfoform').serialize(),
                cache: false,
                type: "POST",
                url: "/PricingRequest/RemoveShippingLocation",
                success: function (response) {
                    $('#shiplocation-wrapper').html(response);
                }
            })
        }
    })
}

Now, lets say I have 4 items created in the user view. I click on item 2 to remove it and it passes in i as an id. I then remove that from the model list and pass back the view with the updated model. I have debugged this at least 50 times now and the controller method looks exactly as it should and the data in the view as well. Inexplicably though the response on the ajax shows differently than what I’m seeing while debugging. Clicking on remove item 2 removes item 4. If I have 5 items, item 5 will be removed no matter what item I click to remove. It’s always the last item that is removed. I know I must be missing something here but I can’t see what. Any ideas? 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-06-13T21:24:51+00:00Added an answer on June 13, 2026 at 9:24 pm

    The problem with your code is that you are using sequential indexes for your collections and not recalculating those indexes when adding/removing elements leaving gaps which prevents the model binder from operating correctly. As an alternative you could use non-sequential indexes as illustrated in the following blog post.

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

Sidebar

Related Questions

So right now I have a partial view that just has a list of
I have a partial view that has a number of Div's, each are set
I have a view partial nameplate.jade that has a couple attributes bound from a
I have a partial view that uses a Telerik MVC Grid, and it has
I'm using MVC2 with VS2010 I have a view that has two partial views
I have a partial view that has something like this <%= Html.DropDownListFor(m => m.SelectedProductName,
I have a partial view (user control) that is shared by my Create and
I have a view that has a partial view in it, everything works fine,
I have a blog that has a sidebar with a partial view in it
I have a partial view that returns an HTML chunk of list items that

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.