1

I have a model:

public class MyModel
{
    public List<Location> Locations { get; set; }
}

Let's say in the list we have 3 items. Then I generate with EditorFor the html for locations:

@Html.EditorFor(a => a.Locations)

The second location was deleted from the html (by javascript I mark the flag for this location as deleted)

In the Action I delete from the list of locations deleted location

model.Locations.RemoveAll(a => a.IsDeleted);

Then I generate the new View with content like this:

@for (int locationIndex = 0; locationIndex < Model.Locations.Count; locationIndex++)
{
    @Html.HiddenFor(m => Model.Locations[locationIndex].Address) <br />
    @Html.HiddenFor(m => Model.Locations[locationIndex].LocationType) <br />
}

Though I CAN'T BELIEVE! When I look on generated html code I see my deleted from the locations list location so I see two locations first and the second. But not first and the third

Please help I have never seen this behavior from MVC. What I am doing wrong?

IMPORTANT UPDATE: Once I replace the @Html.HiddenFor with simple html it works.

    @for (int locationIndex = 0; locationIndex < Model.Locations.Count; locationIndex++)
    {
        <input type="hidden" name="Locations[@locationIndex].Address" value="@Model.Locations[locationIndex].Address" />
        <input type="hidden" name="Locations[@locationIndex].LocationType" value="@Model.Locations[locationIndex].LocationType" />
    }
Sergey
  • 7,933
  • 16
  • 49
  • 77
  • Btw I am on 100 % sure that Locations list cleared correctly. I debugged it. Maybe I need to clear for the second item the ViewData somehow? – Sergey Aug 23 '13 at 19:19
  • `model.Locations.RemoveAll(a => a.IsDeleted);` This line of code - is in the post action of your controller? Is the Locations object updating as you'd expect when you step over it? – User Aug 23 '13 at 19:30
  • Yes this is the POST action. And yes Locations update as supposed. In fact when I set break point debugger shows third location but HiddenFor generates the second (DELETED) location. – Sergey Aug 23 '13 at 19:31

2 Answers2

1

I've fixed it but replacing Html helper with simple html like this:

 @for (int locationIndex = 0; locationIndex < Model.Locations.Count; locationIndex++)
    {
        <input type="hidden" name="Locations[@locationIndex].Address" value="@Model.Locations[locationIndex].Address" />
        <input type="hidden" name="Locations[@locationIndex].LocationType" value="@Model.Locations[locationIndex].LocationType" />
    }
Sergey
  • 7,933
  • 16
  • 49
  • 77
  • Do not do that. Its a `ModelState` issue. You can just use `ModelState.Clear()` before you return the view. But the correct approach is to follow the PRG pattern and redirect to your GET method, not return the view (because its not the same view) –  Jun 16 '18 at 05:48
0

I was having this exact problem too. After removing an item from my list, previously deleted entries would show up. Your solution solved this for me, thank you.

To help clarify:

@Html.HiddenFor(m => Model[i].id)

Was causing issues, but by creating the html markup manually:

<input type="hidden" name="[@i].Id" value="@Model[i].Id" />

Resulted in the deleted fields remaining deleted.

Rixium
  • 86
  • 1
  • 7