0

在 Visual Studio 2013 web express 中使用 MVC 5、EF 6.01 和 Identity。

我认为这将是添加历史表以跟踪定价变化的简单任务。对于父记录的每次保存,都会创建两个子历史记录,但不确定原因。我尝试了下面的方法,也没有使用集合(单独更新的隔离历史表)。还尝试更改控制器中的事件顺序。

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

注意:调试器中的每个 Step Over 都必须单击两次。它几乎就像有两个同时运行的代码实例。跨步时,其他模块和类可以正常工作。

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 回答