我有這樣的問題:ASP.Net MVC 3.0遠程驗證
我確認其中的2個屬性的總和`噸大於100 對於此我使用如下模型:
在我的模型,這些都是我的屬性:
[Remote("ValidatePrevTextWidth", "Validation", AdditionalFields = "TextboxWidth")]
public int PrevTextWidth { get; set; }
[Remote("ValidateTextboxWidth", "Validation", AdditionalFields = "PrevTextWidth")]
public int TextboxWidth { get; set; }
我ValidationController有情況如下:
public JsonResult ValidatePrevTextWidth(int PrevTextWidth, int TextboxWidth)
{
bool valid = AreValid(PrevTextWidth, TextboxWidth);
return Json(valido, JsonRequestBehavior.AllowGet);
}
public JsonResult ValidateTextboxWidth(int TextboxWidth, int PrevTextWidth)
{
bool valid = AreValid(PrevTextWidth, TextboxWidth);
return Json(valido, JsonRequestBehavior.AllowGet);
}
private bool AreValid(int prevTextWidth, int textboxWidth)
{
return (prevTextWidth + textboxWidth)<=100;
}
我的看法如下:
@using (Html.BeginForm("Index", "Pregunta", FormMethod.Post, new { id = "frmNewPreguntaDesign" }))
{ @Html.TextBoxFor(m => m.PrevTextWidth)
@Html.TextBoxFor(m => m.TextboxWidth)
}
工作正常。 的問題如下: 假設由用戶插入的prevTextWidth
是55
,然後他插入46
在textboxWidth
,這裏的驗證在textboxWidth
失敗,它會突出顯示。
但是,如果現在用戶將prevTextWidth的值更改爲54,會發生什麼情況?驗證贏得了t fail, but the
textboxWidth will continue to be highlighted and not valid. The only way to make it valid is to re-insert the value of
textboxWidth`。
那麼有沒有辦法在同一時間驗證兩個屬性,而不是重新插入第二個值使其有效?
由於提前,
馬蒂亞斯
感謝您的快速響應。我已經嘗試過了。 $(「#PrevTextWidth」)。change(function(){ alert(「here」); $('#TextboxWidth')。valid(); });'第一次改變PrevTextWidth時,它驗證這兩個屬性。但以下時間只驗證PrevTextWidth字段。警報消息始終顯示。任何想法爲什麼會發生?謝謝 –
正確的主意,但不幸的是這不會做這項工作。關閉,但看看這裏從@Kiff的完整答案http://stackoverflow.com/questions/10163683/remote-validation-mvc-3-0-multiple-fields-validation – MemeDeveloper