2014-01-30 158 views
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對象和強類型的視圖控制器?

+0

你爲什麼會想這樣做驗證服務器端,驗證了'電子郵件id'需要什麼,我認爲它可能在客戶端本身與正則表達式。因爲服務器驗證會爲簡單驗證創建大量開銷。 – dreamweiver

+0

當然你是對的。但是我想在用戶在瀏覽器中關閉腳本時處理這種情況。所以我想在這裏雙重檢查 – Alexander

+1

'[HttpPost] 公共字符串SendAgreementEmail(字符串jsonOfLog) { //執行驗證 返回轉換。的ToString(jsonOfLog); }',嘗試這樣的,裏面溫控功能執行您的驗證ADN返回相應的消息或錯誤代碼 – dreamweiver

回答

4

我已經使用了與您的代碼相同的代碼,並且服務器端驗證對我來說工作得非常好。爲了簡單起見,我刪除了除電子郵件屬性之外的所有屬性(因爲它導致了主要問題)。

型號 -

public class AgreementReportModel 
{ 
    [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; } 
} 

控制器動作 -

public class AgreementController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public EmptyResult SendAgreementEmail(AgreementReportModel model) 
    { 
     bool t = ModelState.IsValid; 
     return new EmptyResult(); 
    } 
} 

索引視圖 -

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
<script src="~/Scripts/jquery-1.10.2.min.js"></script> 
<script> 
    function SendEmailWithJquery() { 

     var sendData = { 
      EmailTo: '' 
     }; 

     $.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) { 

      } 
     }); 
    }; 
</script> 
<input type="button" value="Click" onclick="SendEmailWithJquery()" /> 

當我開始EmailTo:'[email protected],我得到ModelState.IsValid爲真

enter image description here

當我開始EmailTo:空EmailTo: '',我得到ModelState.IsValid假

enter image description here

。下一步:

  • 檢查您的其他數據類型,它們可能是罪魁禍首。
  • 檢查您的JavaScript輸入檢索,document.getElementById,可以給你的空值。代碼
  • 檢查其他部位,這可能會導致問題,可能是這樣的代碼操作篩選等