How this can be done ?
怎么做到这一点?
For example in my model i have
例如,在我的模型中,我有
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect email")]
public string EmailTo { get; set; }
My current test new controller is
我目前测试的新控制器是
[HttpPost]
public EmptyResult SendAgreementEmail(AgreementReportModel model)
{
bool t = ModelState.IsValid;
return new EmptyResult();
}
ModelState.IsValid is valid even if emailto is null.
即使emailto为null,ModelState.IsValid也是有效的。
And js function
和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) {
}
});
};
Is it possible at all to validate server-side for controller with json object and strong-typed view ?
是否有可能通过json对象和强类型视图验证服务器端的控制器?
1 个解决方案
#1
4
I have used the same code as of yours, and server side validation is working absolutely fine for me. For simplicity sake I removed all properties, except the email property (as it is causing the main problem).
我使用了与您相同的代码,服务器端验证对我来说非常好。为简单起见,我删除了除电子邮件属性之外的所有属性(因为它导致了主要问题)。
Model -
型号 -
public class AgreementReportModel
{
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect email")]
public string EmailTo { get; set; }
}
Controller Action -
控制器动作 -
public class AgreementController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public EmptyResult SendAgreementEmail(AgreementReportModel model)
{
bool t = ModelState.IsValid;
return new EmptyResult();
}
}
Index View -
索引视图 -
@{
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()" />
When I initiated EmailTo : 'rami@gmail.com, I get ModelState.IsValid to true.
当我发起EmailTo:'rami@gmail.com时,我得到ModelState.IsValid为true。
When I initiated EmailTo : null or EmailTo : '', I get ModelState.IsValid to false.
当我启动EmailTo:null或EmailTo:''时,我将ModelState.IsValid设置为false。
What to do next:
接下来做什么:
- Check your other data types, they might be culprits.
- 检查您的其他数据类型,他们可能是罪魁祸首。
- Check you JavaScript input retrieval, document.getElementById, that can be giving your nulls.
- 检查JavaScript输入检索,document.getElementById,它可以给出空值。
- Check other parts of code, which can cause the problem, probably code like action filters etc.
- 检查可能导致问题的其他代码部分,可能是代码,如动作过滤器等。
#1
4
I have used the same code as of yours, and server side validation is working absolutely fine for me. For simplicity sake I removed all properties, except the email property (as it is causing the main problem).
我使用了与您相同的代码,服务器端验证对我来说非常好。为简单起见,我删除了除电子邮件属性之外的所有属性(因为它导致了主要问题)。
Model -
型号 -
public class AgreementReportModel
{
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect email")]
public string EmailTo { get; set; }
}
Controller Action -
控制器动作 -
public class AgreementController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public EmptyResult SendAgreementEmail(AgreementReportModel model)
{
bool t = ModelState.IsValid;
return new EmptyResult();
}
}
Index View -
索引视图 -
@{
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()" />
When I initiated EmailTo : 'rami@gmail.com, I get ModelState.IsValid to true.
当我发起EmailTo:'rami@gmail.com时,我得到ModelState.IsValid为true。
When I initiated EmailTo : null or EmailTo : '', I get ModelState.IsValid to false.
当我启动EmailTo:null或EmailTo:''时,我将ModelState.IsValid设置为false。
What to do next:
接下来做什么:
- Check your other data types, they might be culprits.
- 检查您的其他数据类型,他们可能是罪魁祸首。
- Check you JavaScript input retrieval, document.getElementById, that can be giving your nulls.
- 检查JavaScript输入检索,document.getElementById,它可以给出空值。
- Check other parts of code, which can cause the problem, probably code like action filters etc.
- 检查可能导致问题的其他代码部分,可能是代码,如动作过滤器等。