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

Remote validation causes submit inputs (multiple-submit-buttons) to be null

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

视图模型:

[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)


<button class="form-button-submit" type="submit" name="Send" value="Send">Send</button>

验证工作完美。

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

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

The issue happens when a form which uses the remote method to validate a field. If a submit button is pressed after the validator fires, everything is alright and the request will contain the clicked submit button name/value pair. However, when the remote method is not fired before the submit button is pressed, then the resulting request will NOT contain the submit button value/pair.

对我有用的解决方案是这个:

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

归功于arturoribes