Lifes
Lifes

Reputation: 1317

ASP.NET MVC 3 JQuery POST and GET - Partial Views not Updating Properly

EDIT: I have now discovered the problem is with my WCF service getting dirty reads. Please see my other post. I'd still appreciate any advice on stuff I can improve in this post (such as a simple spelling mistake on my JS or a better way to update a partial view).

Other post: Linq to SQL - Dirty Reads after Updating - WCF Service

EDIT: Ok I've narrowed it down to a problem on the web service. I wrote a small application that uses my service reference to read and update Locations. Sometimes when I read a location, it Location name will change to an old name and I'll get the error ChangeConflictException: row not found or changed.

Ok basically I have an MVC view with a form and 2 partial views (a drop down list and a DevExpress MVC Extensions TreeView). When a user clicks on a node, it calls a client side JS function that makes an Ajax POST to the server, gets the Node details (from the JSON result), and populates the form.

When the user clicks 'Submit/Save', another JS function is called and it also makes an Ajax POST to the server, passing in the node info. When that post returns with a JSON result, a JQuery GET request is made to the server. It gets the partial view as an html string, then replaces the TreeView with the new HTML.

This all seems to be working, except if I change a Node name from Node1 to Node2 to Node3, I sometimes get Node1 in the TreeView, Node3 in the DropDown, and Node2 in the form when I click on this "Node1" in the TreeView. Yet it's all coming from the database, which has the correct information. And each time a Node is clicked or saved, the Model gets refreshed with the latest data from the database. When I check in the code behind, sometimes that updated model is wrong (e.g. says Node2 instead of Node3).

Why is this happening? I've tried clearing my browser cache, adding no-cache attributes to my controller (from other Stack Overflow questions). I have no idea what is going on! Sometimes if I just keep hitting refresh on the page, the tree view and drop down list change to previous values (e.g. Node1)!

Note: Node == Location == Unit (the DB table/entity I'm working with).

Note: the database has a trigger on the Location (the Nodes of the tree).

Note: the server uses WCF to communicate to the database.

Note: the .Using for the service is an extension that makes sure to properly closes the service client.

Edit: Removed code at the request of my co-worker.

Upvotes: 1

Views: 846

Answers (2)

Lifes
Lifes

Reputation: 1317

I resolved this. It was caused by caching on Structure Map that only became apparent when using WCF (because of WCF's caching).

Please see my other post: Linq to SQL - Dirty Reads after Updating - WCF Service

Upvotes: 0

JTew
JTew

Reputation: 3179

If you have copy and pasted the js I think this line is wrong:

contentType: "application/json; chartset=utf-8",

Should be:

contentType: "application/json; charset=utf-8",

Its not immediately clear to me if this would break what you are trying to do.

Upvotes: 1

Related Questions