0
我在我的頁面上有幾個input
類型的submit
字段。對於其中的一些,我希望驗證是正常發生的,但其他人則不會。每個submit
按鈕將表單發回控制器,爲每個按鈕設置一個命令值。這允許該動作執行相關功能。但是,如果將類cancel
添加到相關的input
標記中,則不會對它們進行驗證。但是,我沒有獲得發回控制器操作的命令的值。所以我可以不驗證或得到一個命令值,但不是兩者。停止從特定提交按鈕的觸發驗證
設置;
模型;
public void SubData
{
public int ID { get; set; }
}
public void SomeViewModel
{
public int ID { get; set; }
public List<SubData> Data { get; set; }
}
在HTML中;
<input type="submit" name="command" value="AddData" />
<input type="submit" name="command" value="DeleteData/>
<input type="submit" name="command" value="SaveAll/>
控制器;
public ActionResult Index(SomeViewModel model, string command)
{
switch (command)
{
case "AddData":
model.Stuff.Add(new SubData());
break;
case "DeleteData":
// you get the idea...
break;
}
}
Javascript(taken from:https://stackoverflow.com/a/17401660);
$(function() {
// allow delete actions to trigger a submit without activating validation
$(document).on('click', 'input[type=submit].cancel', function (evt) {
// find parent form, cancel validation and submit it
// cancelSubmit just prevents jQuery validation from kicking in
$(this).closest('form').validate().cancelSubmit = true;
$(this).closest('form').submit();
return false;
});
});
所以,如果我有<input type="submit" class="cancel" value="AddSomething" />
然後確認不會發生(耶!),但我沒有得到任何的動作的參數command
提供的值(噓!)。取出cancel
觸發器驗證,一旦驗證成功,我會得到command
的值。
的問題不在於我不能關閉驗證輸入,它是這樣做停止'input'價值的subimition從發佈到控制器操作 – DiskJunky