2
這是怎麼做到的?mvc服務器端驗證jquery ajax發佈請求
例如在我的模型,我有
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect email")]
public string EmailTo { get; set; }
我現在測試新的控制器是
[HttpPost]
public EmptyResult SendAgreementEmail(AgreementReportModel model)
{
bool t = ModelState.IsValid;
return new EmptyResult();
}
ModelState.IsValid是即使emailto爲null有效。
和JS功能
function SendEmailWithJquery(offerID, agreementOfferID, customerID) {
var emailTo = document.getElementById('txtEmailTo').value;
var emailSubject = document.getElementById('txtEmailSubject').value;
var emailBody = document.getElementById('txtEmailBody').value;
var sendData = {
OfferID: offerID,
AgreementOfferID: agreementOfferID,
CustomerID: customerID,
EmailTo: emailTo,
EmailSubject: emailSubject,
EmailBody: emailBody
};
$.ajax({
url: "/Agreement/SendAgreementEmail",
type: "POST",
data: JSON.stringify(sendData),
dataType: 'json',
contentType: 'application/json',
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
};
是否有可能在所有的驗證服務器端與JSON對象和強類型的視圖控制器?
你爲什麼會想這樣做驗證服務器端,驗證了'電子郵件id'需要什麼,我認爲它可能在客戶端本身與正則表達式。因爲服務器驗證會爲簡單驗證創建大量開銷。 – dreamweiver
當然你是對的。但是我想在用戶在瀏覽器中關閉腳本時處理這種情況。所以我想在這裏雙重檢查 – Alexander
'[HttpPost] 公共字符串SendAgreementEmail(字符串jsonOfLog) { //執行驗證 返回轉換。的ToString(jsonOfLog); }',嘗試這樣的,裏面溫控功能執行您的驗證ADN返回相應的消息或錯誤代碼 – dreamweiver