我有一个模特:
public class MyModel
{
public List<Location> Locations { get; set; }
}
我们在列表中说我们有3个项目。然后我使用EditorFor html创建位置:
@Html.EditorFor(a => a.Locations)
第二个位置已从html中删除(通过javascript我将此位置的标记标记为已删除)
在动作中我从已删除位置列表中删除
model.Locations.RemoveAll(a => a.IsDeleted);
然后我使用以下内容生成新视图:
@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 />
}
虽然我不相信!当我查看生成的HTML代码时,我看到我从位置列表位置删除了所以我看到了第一个和第二个位置。但不是第一和第三
请帮助我从未见过MVC的这种行为。我做错了什么?
重要更新: 一旦我用简单的html替换@ Html.HiddenFor就行了。
@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" />
}
答案 0 :(得分:1)
我已经修复了它,但用简单的html取代了Html helper:
@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" />
}
答案 1 :(得分:0)
我也遇到了这个问题。 从我的列表中删除项目后,将显示以前删除的条目。 你的解决方案为我解决了这个问题,谢谢你。
帮助澄清:
@Html.HiddenFor(m => Model[i].id)
导致问题,但通过手动创建html标记:
<input type="hidden" name="[@i].Id" value="@Model[i].Id" />
导致已删除的字段被删除。