我正在将现有门户从 Web 表单重新构建为 MVC,但我在验证时遇到了问题。我有这样的模型:
public class Customer
{
[RequiredIf("FirstNameRequired", true)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
public bool FirstNameRequired { get; set; }
public bool FirstNameVisible { get; set; }
[RequiredIf("LastNameRequired", true)]
public string LastName { get; set; }
public bool LastNameRequired { get; set; }
public bool LastNameVisible { get; set; }
[RequiredIf("EmailRequired", true)]
public string Email { get; set; }
public bool EmailRequired { get; set; }
public bool EmailVisible { get; set; }
}
public class PersonalInfo
{
public Customer PrimaryCustomer { get; set; }
public IEnumerable<Customer> Customers { get; set; }
}
客户中有很多属性,但我仅添加了 3 个(例如 100 个或其他)。我这样做是因为有许多联系人类型和属性,例如可见和必需是 dinamyc。现在我有了可以呈现所有客户的编辑器模板。它是这样的。
@if (Model.FirstNameVisible)
{
<p>
First Name:
@Html.TextBoxFor(x => x.FirstName)
</p>
}
@if (Model.LastNameVisible)
{
<p>
Last Name:
@Html.TextBoxFor(x => x.LastName)
</p>
}
@if (Model.EmailVisible)
{
<p>
Email:
@Html.TextBoxFor(x => x.Email)
</p>
}
现在这是渲染所有联系人,但是当我在模型中发布数据时 REQUIRED 和 VISIBLE 布尔属性为假。这是因为我没有添加:
@Html.HiddenFor(x => x.FirstNameRequired)
@Html.HiddenFor(x => x.FirstNameVisible)
如果我添加此验证效果很好。ModelState.IsValid 工作正常。问题是如果有人更改隐藏的值数据并且能够插入不正确的数据怎么办。我怎样才能防止这种情况?