我有一个模型,其中包含一个地址和人员两次,一次为“主要”联系人,一次为“发票”联系人,和一个名为InvoiceContactSameAsMain的布尔值 - 一个笨拙的名字,但描述性的。属性的getter会检查“main”和“invoice”的Address和Contact对象是否相同,如果是,则返回true。设置人员检查该值是否为真,如果是,则将主要人员复制到发票人员上,并将主要地址复制到发票地址上。有没有办法强制模型在MVC3控制器中重新验证?
在我的视图中,布尔值由复选框表示(如您所期望的)。附加的是一个小的JS函数,如果选中该复选框,则隐藏发票字段,并通过将data-val HTML属性设置为false并强制重新分析不显眼验证属性的形式。取消选中该框自然显示字段并重新打开验证。
所有这些工作正常,直到我到我的控制器。
尽管模型是“有效的”并包含正确的字段(感谢我的InvoiceContactSameAsMain setter),但ModelState.IsValid仍然坚决不实,并且我似乎无法找到任何方法来重新验证模型。如果我清除模型状态,任何和所有的错误消失。我非常想避免在名称中挖掘ModelState中的字段,因为Person和Address对象在整个项目中都会使用,并且可能需要在某个时间点更改或扩展。
有没有什么明显的我在这里错过了,这将允许我重新验证ModelState?我试过TryUpdateModel和TryValidateModel,但它们都显示使用缓存的ModelState值。我甚至尝试递归调用我的Action,传入“固定”模型。我几乎感谢一个人没有工作。
请让我知道,如果有任何更多的细节或例子将有所帮助。
编辑:显然,如果这是完全错误的方式来解决问题,请让我知道。
编辑2:根据Ron Sijm的建议添加代码示例。
该模型如下: public class详细信息 { public int? UserID {get;组; }
public Company Company { get; set; }
public Address CompanyAddress { get; set; }
public Person MainPerson { get; set; }
public Address InvoiceAddress { get; set; }
public Person InvoiceContact { get; set; }
[Display(Name = "Promotional code")]
[StringLength(20, ErrorMessage = "Promotional code should not exceed 20 characters")]
public string PromotionalCode { get; set; }
[Display(Name = "Invoice contact same as main")]
public bool InvoiceContactSameasMain
{
get { return InvoiceContact.Equals(MainPerson); }
set
{
if (value)
{
InvoiceContact = MainPerson.Copy();
InvoiceAddress = CompanyAddress.Copy();
}
}
}
[_Common.MustAccept]
[Display(Name = "I agree with the Privacy Policy")]
public bool PrivacyFlag { get; set; }
[Display(Name = "Please subscribe to Sodexo News Letter")]
public bool MarketingOption { get; set; }
[Display(Name = "Contract number")]
public int? ContractNumber { get; set; }
public Details()
{
Company = new Company();
CompanyAddress = new Address();
MainPerson = new Person();
InvoiceAddress = new Address();
InvoiceContact = new Person();
}
}
这是包裹在一个视图模型因为有一些参与的页面SelectLists的:
public class DetailsViewModel
{
public Details Details { get; set; }
public SelectList MainContactTitles { get; set; }
public SelectList InvoiceContactTitles { get; set; }
public SelectList SICCodes { get; set; }
public SelectList TypesOfBusiness { get; set; }
public SelectList NumbersOfEmployees { get; set; }
public DetailsViewModel()
{
}
}
该控制器的两个相关的操作如下:
public class DetailsController : _ClientController
{
[Authorize]
public ActionResult Index()
{
DetailsViewModel viewModel = new DetailsViewModel();
if (Client == null)
{
viewModel.Details = DetailsFunctions.GetClient((int)UserId, null);
}
else
{
viewModel.Details = DetailsFunctions.GetClient((int)UserId, Client.ContractNumber);
}
viewModel.MainContactTitles = DetailsFunctions.GetTitles((int)UserId, viewModel.Details.MainPerson.title);
viewModel.InvoiceContactTitles = DetailsFunctions.GetTitles((int)UserId, viewModel.Details.InvoiceContact.title);
viewModel.SICCodes = DetailsFunctions.GetSICCodes(viewModel.Details.Company.sic_code);
viewModel.NumbersOfEmployees = DetailsFunctions.GetNumbersOfEmployees(viewModel.Details.Company.number_of_employees);
viewModel.TypesOfBusiness = DetailsFunctions.GetTypesOfBusiness(viewModel.Details.Company.public_private);
return View(viewModel);
}
[Authorize]
[HttpPost]
public ActionResult Index(DetailsViewModel ViewModel)
{
if (ModelState.IsValid)
{
//go to main page for now
DetailsFunctions.SetClient((int)UserId, ViewModel.Details);
return RedirectToAction("Index", "Home");
}
else
{
ViewModel.MainContactTitles = DetailsFunctions.GetTitles((int)UserId, ViewModel.Details.MainPerson.title);
ViewModel.InvoiceContactTitles = DetailsFunctions.GetTitles((int)UserId, ViewModel.Details.InvoiceContact.title);
ViewModel.SICCodes = DetailsFunctions.GetSICCodes(ViewModel.Details.Company.sic_code);
ViewModel.NumbersOfEmployees = DetailsFunctions.GetNumbersOfEmployees(ViewModel.Details.Company.number_of_employees);
ViewModel.TypesOfBusiness = DetailsFunctions.GetTypesOfBusiness(ViewModel.Details.Company.public_private);
return View(ViewModel);
}
}
}
我可以提供视图和JS,如果需要的话,但作为模型绑定都工作得很好,我不知道这是多少帮助。
我认为你的代码示例有帮助 –
你可以在调试时看到为什么'ModelState.IsValid'是错误的。如果展开一些节点,您将看到哪些规则失败。 – Darcy
@Darcy:失败的规则是复制的发票地址和个人中的规则。验证器仍然将它们视为空白,尽管我可以在模型中正确地看到它们。 –