在我的项目中,我有一个模型,你可以在这里看到我模型的一部分:
public class CheckoutModel
{
public bool OtherPlace { get; set; }
[RequiredIf("OtherPlace", true, ErrorMessage = " ")]
public string OtherPlaceFullName { get; set; }
[RequiredIf("OtherPlace", true, ErrorMessage = " ")]
public int OtherPlaceProvinceId { get; set; }
[RequiredIf("OtherPlace", true, ErrorMessage = " ")]
public string OtherPlaceCity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我使用RequiredIf属性在视图中验证我的模型,
if (!ViewData.ModelState.IsValid)
{
@Html.ValidationSummary(false)
}
Run Code Online (Sandbox Code Playgroud)
我填写了我的表单的所有属性,但是当未填充OtherPlaceProvinceId时,我得到以下验证错误.
值"on"对于OtherPlace无效.
更新:控制器在这里:
[HttpGet]
public ActionResult CheckoutAccount()
{
var model = OrderManager.Instance.GetCheckoutAccount();
return View("_CheckoutAccount", model);
}
[HttpPost]
public …Run Code Online (Sandbox Code Playgroud)