I have a problem that is getting embarrassingly out of hand.
I’m working with MVC and EF and have a model that the user can edit. I just got a new requirement that if it is edited, the edited property shall appear in red in the view for other users to see.
My model has a lot of properties, but the primary key is a GUID, and the model can have multiple parentIDs. So, for one a given GUID and ParentID I want to find what properties have been changed.
So far, which feels wrong, I have some jquery in my view that does the following
$("[name*='Request.']")
.not(":hidden")
.each(function () {
var $this = $(this);
$.ajax({
url: '@Url.Action("IsChanged", "Shared")',
data: { fieldName: $this.attr("name"), parentID: @Model.Request.ID },
success: function (data) {
if (data) {
if ($this.is(":radio")) {
$this.parent().addClass("isChanged");
} else
$this.addClass("isChanged");
}
}
});
});
This script works fine, if I try it with simply returning true from my ajax function. However, it’s the ajax function that is the problem. How do I find the changes? What I got so far is this:
public ActionResult IsChanged(string fieldName, int parentID)
{
using (MyEntities _db = new MyEntities())
{
string[] aName = fieldName.Split('.');
// Count the number of version
int versionCount = _db.SubRequests.Count(r => r.ParentID == parentID);
// Get the first version
SubRequest sr = _db.SubRequests.FirstOrDefault(r => r.ParentID == parentID);
// Check if the count is the same for the specific value
int changeCount = _db.ExecuteStoreQuery<int>("select count(parentID) from "+aName[0]+" where parentID = " + parentID + " and " + aName[1] +"='{0}'", THEVALUEINORIGINAL);
bool isChanged = changeCount == versionCount;
return Json(isChanged, JsonRequestBehavior.AllowGet);
}
}
What I’ve done so far doesn’t feel right, and obviously this doesn’t work, but I’m lost.
Let me know if this is understandable, or if you need more information. Should I scrap this idea and build something completely different (probably), or is there a way to save me?
I’m sure it’s not the prettiest way to do it, but I thought I’d share what solution I finally decided on, in the end, in case anyone else would ever be looking to do something similar.
My jQuery ajax function remains the same, but in the controller I have the following:
Works fine for me. However, it’s a pretty heavy loading page, with all those ajax calls.