有没有办法强制在MVC3控制器中重新验证模型?

时间:2011-11-10 15:52:34

标签: asp.net-mvc-3 validation controller

我有一个模型,其中包含一个地址和人员两次,一次用于“主要”联系人,一次用于“发票”联系人,以及一个名为InvoiceContactSameAsMain的布尔值 - 一个笨拙的名称,但具有描述性。属性的getter检查“main”和“invoice”的Address和Contact对象是否相同,如果是,则返回true。 setter检查值是否为true,如果是,则将主Person复制到发票Person上,将主地址复制到发票Address。

在我的视图中,布尔值由复选框表示(正如您所期望的那样)。附加一个小的JS函数,如果选中该复选框,则隐藏发票字段并通过将data-val HTML属性设置为false并强制重新解析不显眼的方式来“关闭”客户端验证。表单中的验证属性。取消选中此框会自然显示字段并重新打开验证。

所有这一切都很好,直到我到达我的控制器。

尽管模型是“有效的”并且包含正确的字段(感谢我的InvoiceContactSameAsMain setter),但ModelState.IsValid仍然是非常错误的,我似乎无法找到任何重新验证模型的方法。如果我清除ModelState,任何和所有错误都会消失。我宁愿避免通过名称挖掘ModelState中的字段,因为Person和Address对象在整个项目中使用,可能需要在某些时候进行更改或扩展。

我在这里错过了一些可以让我重新验证ModelState的东西吗?我已经尝试过TryUpdateModel和TryValidateModel,但它们似乎都使用缓存的ModelState值。我甚至尝试过再次调用我的Action,传递“固定”模型。我差点感谢一个人没有工作。

如果有任何更多细节或示例有帮助,请与我们联系。

编辑:显然,如果这完全是解决问题的错误方法,请告诉我。

编辑2:根据Ron Sijm的建议添加了代码示例。

模型如下:     公共课详情     {         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();
    }
}

这包含在ViewModel中,因为页面中包含许多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,但由于模型绑定工作得很好,我不确定它有多大帮助。

1 个答案:

答案 0 :(得分:3)

这是一个适度的废话,但我最终只是在检查ModelState.IsValid之前清除控制器中相关字段的ModelState错误:

if(ViewModel.Details.InvoiceContactSameasMain)
        {
            //iterate all ModelState values, grabbing the keys we want to clear errors from
            foreach (string Key in ModelState.Keys)
            {
                if (Key.StartsWith("Details.InvoiceContact") || Key.Startwith("Details.InvoiceAddress"))
                {
                    ModelState[Key].Errors.Clear();
                }
            }
        }

唯一的好处是,如果Person或Address对象发生变化,则不需要更改此代码。