Model view controller 远程验证导致提交输入(多个提交按钮)为空

Model view controller 远程验证导致提交输入(多个提交按钮)为空,model-view-controller,unobtrusive-javascript,remote-validation,Model View Controller,Unobtrusive Javascript,Remote Validation,我最近在我的表单中实现了远程验证: 视图模型: [Remote("IsTagUnique", "myController", "myArea", ErrorMessage = "This tag already exists.")] public string tag { get; set; } 控制器: public ActionResult IsTagUnique(string tag) { using (db)

我最近在我的表单中实现了远程验证:

视图模型:

[Remote("IsTagUnique", "myController", "myArea", ErrorMessage = "This tag already exists.")]
public string tag { get; set; }
控制器:

        public ActionResult IsTagUnique(string tag)
        {
            using (db)
            {
                try
                {
                    var myTag = db.ASAuftraege.Single(m => m.tag == tag);
                        return Json(false, JsonRequestBehavior.AllowGet);
                    }                        
                }
                catch (Exception)
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult myView(string Send){ 
            // doSomething 
        }
视图(称为“myView”)

@Html.TextBoxFor(m=>m.tag)@Html.ValidationMessageFor(m=>m.tag)
邮寄
验证工作非常好


问题是:当我单击Send按钮而没有手动触发tag字段上的验证时,通过单击该字段,然后单击其他地方,在myView()函数之前执行“IsTagUnique”函数。这会导致提交输入(实际上我有多个发送按钮,就像视图中显示的按钮一样(当然名称/值不同)设置为空。知道我能做什么吗?我尝试通过提供焦点和模糊标记字段以及触发更改事件手动触发验证。但不会触发验证。

搜索一段时间后,我发现这似乎是一个已知的错误:

当使用远程方法验证字段的表单时,会发生此问题。如果在验证程序启动后按下提交按钮,则一切正常,请求将包含单击的提交按钮名称/值对。 但是,如果在按下submit按钮之前未触发远程方法,则生成的请求将不包含submit按钮值/对

一个对我有效的解决方案是:

$(function() {
$('button[type=submit]').click(function () {
    $('<input>').attr({
        type: 'hidden',
        name: this.name,
        value: this.value
    }).appendTo($(this).closest('form'));
});
});
$(函数(){
$('button[type=submit]')。单击(函数(){
$('').attr({
键入:“隐藏”,
姓名:this.name,
值:这个值
}).appendTo($(this.closest('form'));
});
});
归功于

$(function() {
$('button[type=submit]').click(function () {
    $('<input>').attr({
        type: 'hidden',
        name: this.name,
        value: this.value
    }).appendTo($(this).closest('form'));
});
});