I use an ajax form for removing items from a list. The first time I submit something, it works but the second times, the reference of the item submitted is not correct: it is the first reference that is still used.
Here is my ajax form:
<div>
<table>
@foreach (var item in Model.ProjectTechnology)
{
<tr>
<td>@Html.DisplayFor(m => item.TechnologyID) </td>
<td>@using (Ajax.BeginForm("RemoveLinkedTechnology", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "AddedTechnologies" })) {
@Html.Hidden("projectID", item.ProjectID)
@Html.Hidden("removedTechnologyID", item.TechnologyID)
<input type="submit" value="Suppr" />
}</td>
</tr>
}
</table>
</div>
Here is the action in my controller:
[HttpPost]
public ActionResult RemoveLinkedTechnology(int projectID, string removedTechnologyID)
{
// some code here...
}
Example:
Lets say I proceed the submitting like this: first submit: AA; second submit: BB.
For the first call: removedTechnologyID contains AA.
For the second call: removedTechnologyID still contains AA.
Any idea?
Thanks
I suspect that in your controller action you are returning a partial view which updates the contents of the
<table>you have shown. Now since Html helpers such as Hidden or TextBox first look for values in ModelState before binding and then in the model what happens is that@Html.Hidden("removedTechnologyID", item.TechnologyID)sees that there is aremovedTechnologyID="AA"in the model state and completely ignores your model value which isitem.TechnologyID. So if you have looked at the DOM after the first AJAX request you would have seen that all hidden fields have the old values inside them.To fix this you have 3 possibilities:
Clear the item in model state in your controller action:
Don’t use helpers to generate the hidden fields:
Write a custom
Html.Hiddenhelper which will first use the values in the model before looking at modelstate (out of scope for this answer)