2014-01-30 65 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,可以给你的空值。代码
  • 检查其他部位,这可能会导致问题,可能是这样的代码操作筛选等