1

恥ずかしそうに手に負えなくなっている問題があります。

私はMVCとEFを使用しており、ユーザーが編集できるモデルを持っています。編集した場合、編集したプロパティが他のユーザーに表示されるようにビューに赤で表示されるという新しい要件があります。

私のモデルには多くのプロパティがありますが、主キーはGUIDであり、モデルには複数parentIDのを含めることができます。したがって、ある特定のプロパティについて、変更されたプロパティを見つけたいと思いますGUIDParentID

これまでのところ、これは間違っていると感じていますが、私のビューには次のようなjqueryがあります

$("[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");
                    }
                }
            });
        });

このスクリプトは、ajax関数からtrueを返すだけで試してみると、正常に機能します。ただし、問題となるのはajax関数です。変更を見つけるにはどうすればよいですか?私がこれまでに得たものはこれです:

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);
        }
    }

私がこれまでに行ったことは正しくないと感じており、明らかにこれは機能しませんが、私は道に迷っています。

これが理解できるかどうか、またはさらに情報が必要な場合はお知らせください。このアイデアを破棄して、まったく異なるものを構築する必要がありますか(おそらく)、それとも私を救う方法はありますか?

4

2 に答える 2

0

同時実行の競合を正しく処理する方法を確認することをお勧めします。この記事を見てください。

于 2012-06-26T19:00:34.840 に答える
0

それが最も美しい方法ではないと確信していますが、他の誰かが同じようなことをしようとしている場合に備えて、最終的に私が最終的に決定した解決策を共有したいと思いました。

私のjQueryajax関数は同じままですが、コントローラーには次のものがあります。

public ActionResult IsChanged(string fieldName, int parentID)
{
    using (MyEntities _db = new MyEntities())
    {
        bool isChanged = false; 

            string[] aName = fieldName.Split('.');

            string strTableName = aName[0];
            string strFieldName = aName[1];

            string strQuery = String.Format("select distinct {0} from {1} where parentID = {2}", strFieldName, strTableName, parentID);

            isChanged = _db.ExecuteStoreQuery<dynamic>(strQuery).Count() > 1;

            return Json(isChanged, JsonRequestBehavior.AllowGet);
    }
}

私にとってはうまくいきます。ただし、これはかなり重い読み込みページであり、これらすべてのajax呼び出しが含まれています。

于 2012-06-28T14:52:13.430 に答える