0

Visual Studio 2013 Web Express で MVC 5、EF 6.01、および Identity を使用します。

これは、価格の変化を追跡するための履歴テーブルを追加するだけの簡単な作業だと思いました。親レコードを保存するたびに、2 つの子履歴レコードが作成されますが、その理由は不明です。以下のアプローチを試しましたが、コレクション (個別に更新される独立した履歴テーブル) も使用しませんでした。また、コントローラーのイベントの順序を変更してみました。

 private AppDb db = new AppDb();
 private UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());

 public class Threshold
 {
    public int ThresholdId { get; set; }
    public string ItemId { get; set; }
    public string OfficeCode { get; set; }
    public string Unit { get; set; }
    public string Type { get; set; }
    public string Color { get; set; }
    public string Frequency { get; set; }
    public decimal Price { get; set; }
    public decimal OldPrice { get; set; }
    public int? Volume { get; set; }
    public int? Sort { get; set; }
    public DateTime? DateModified { get; set; }
    public virtual ICollection<Threshold_History> Threshold_History { get; set; }
}

public class Threshold_History
{
    public int Threshold_HistoryId { get; set; }
    public int ThresholdId { get; set; }
    public decimal PriceFrom { get; set; }
    public decimal PriceTo { get; set; }
    public DateTime DateCreated { get; set; }
    public string UserId {get; set;}
    public virtual Threshold Threshold { get; set; }
}

コントローラー:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult  Edit([Bind(Include="ThresholdId,ItemId,OfficeCode,Unit,Type,Color,Frequency,Price,Volume,Sort,OldPrice")] Threshold threshold)
{
    if (ModelState.IsValid)
    {
        db.Entry(threshold).State = EntityState.Modified;

        //update history table
        Threshold_History hist = new Threshold_History();
        List<Threshold_History> histories = new List<Threshold_History>();
        hist.ThresholdId  = threshold.ThresholdId;
        hist.PriceFrom = threshold.OldPrice;
        hist.PriceTo = threshold.Price;
        hist.DateCreated =  DateTime.Now;
        hist.UserId = User.Identity.GetUserId();

        histories.Add(hist);

        threshold.DateModified = DateTime.Now;
        threshold.Threshold_History = histories;

        db.SaveChanges();

        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }
}

1 つの注意: デバッガーの各ステップ オーバーは 2 回クリックする必要があります。同時に実行されているコードの 2 つのインスタンスがあるようです。他のモジュールとクラスは、ステップ オーバー時に正しく機能します。

EDIT を呼び出して、Edit の呼び出し方法を追加します。これは、モーダル ダイアログでの ajax 呼び出しからのものです。

//modal functions
 function editItem(e){
 e.preventDefault(); 
 $('#dialogContent').load(this.href, function () {
    $('#dialogDiv').modal({
        backdrop: 'static',
        keyboard: true
    }, 'show');
    bindForm(this);
});
return false;
};

function bindForm(dialog) {
$('form', dialog).submit(function () {

    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function (result) {
            if (result.success) {
                $('#dialogDiv').modal('hide');
                 loadTab(tabRef);
            } else {
                $('#dialogContent').html(result);
                $('#dialogDiv').modal({
                    backdrop: 'static',
                    keyboard: true
                }, 'show');
                bindForm();
            }
        },
        error: function (requestObject, error, errorThrown) {
            $('#modalError').html("<br><p>" + errorThrown + "</p> ");
        }
    });
    return false;
});
}

function validateModal() {
var form = $(".modal-form");
$.validator.unobtrusive.parse(form);
if (form.valid()) {
    form.submit();
}
}
4

1 に答える 1

3

validateModal()フォームの送信ボタンから呼び出しますか?送信ボタンが呼び出さ れてフォームが投稿され、送信ボタンがデフォルトの機能であるフォームの投稿を実行する可能preventDefault性はほとんどありません。それは重複した呼び出しを説明します。return falsevalidateModal

送信を自分で処理する場合は、送信ボタンのタイプを「ボタン」に設定して、自動的に送信されないようにする必要があります。

于 2013-11-13T07:28:06.520 に答える