如何在ASP.NET MVC4中验证除当前上下文之外的模型 [英] How to validate model other than in current context in ASP.NET MVC4
本文介绍了如何在ASP.NET MVC4中验证除当前上下文之外的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由三个组成的聚合模型:
I've an aggregate model composed of three:
public class Shipments
{
public Shipment Shipment { get; set; }
public AirInvoice AirInvoice { get; set; }
public SeaInvoice SeaInvoice { get; set; }
}
[HttpPost]
public ActionResult Create(Shipments shipmentModel)
{
//ToDo - Validate shipmentModel.Shipment
}
我正在尝试使用DataAnnotations ValidationContext验证它,但无法:
I'm trying to validate it using DataAnnotations ValidationContext but not being able to:
var context = new ValidationContext(shipmentModel.Shipment); //Error Here. validation context has 3 parameters but invoked with one parameter, though I'm able to see there's a constructor with one parameter also in ValidationContext.cs and recently I was able to do the same but now it's not working... don't know why.
var results = new List<ValidationResult>();
bool LoginModelIsValid = Validator.TryValidateObject(loginModel, context,results,true);
推荐答案
我是如何解决的:
最后几个小时后,我发生了检查是否有任何版本不兼容,并且它确实有效!
我的项目之前的目标是.Net Framework 4.0。当我把它改成4.5时,它开始正常工作
y。
希望它对某人有所帮助,谢谢。
How I solved it:
Finally after few hours it I happen to check if any version incompatibility was there, and it did the trick!
It was .Net Framework 4.0 my project was targeting before. When I changed it to 4.5, it started working properl
y.
Hope it helps somebody, thanks.
这篇关于如何在ASP.NET MVC4中验证除当前上下文之外的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文