tags:

views:

56

answers:

2

Hi,

I have a usercontrol that is rendering a list of items. Each row contains a unique id in a hidden field, a text and a delete button. When clicking on the delete button I use jquery ajax to call the controller method DeleteCA (seen below). DeleteCA returns a new list of items that replaces the old list.

[HttpPost]
public PartialViewResult DeleteCA(CAsViewModel CAs, Guid CAIdToDelete)
{
    int indexToRemove = CAs.CAList.IndexOf(CAs.CAList.Single(m => m.Id == CAIdToDelete));
    CAs.CAList.RemoveAt(indexToRemove);
    return PartialView("EditorTemplates/CAs", CAs);
}

I have checked that DeleteCA is really removing the correct item. The modified list of CAs passed to PartialView no longer contains the deleted item.

Something weird happens when the partial view is rendered. The number of items in the list is reduced but it is always the last element that is removed from the list. The rendered items does not correspond to the items in the list/model sent to PartialView.

In the usercontrol file (ascx) I'm using both Model.CAList and lambda expression m => m.CAList.

How is it possible for the usercontrol to render stuff that is not in the model sent to PartialView?

Thanx Andreas

+1  A: 

It sounds like the ModelState is the trouble here, as you bind to CAs the ModelState save this values in the background as Attempted Values, so its true the object is no longer present at the Model, but the ModelSate still have the values of the deleted object. You can try a:

ModelState.Clear();

To remove all those old values.

Omar
Ahh! Thanks a lot Omar!
Andreas
A: 

Check in firebug what the response realy is. This way you can see if you have a serverside problem or it is a jquery issue.

Malcolm Frexner
Thanks Malcolm. I'm debugging the callback function in javascript and it is clear that the html returned contains the errors. It is a server side problem.
Andreas