我试图让我的脑袋为什么(数据注释)验证错误触发页面第一次加载之前,任何提交/帖子。但更重要的是如何解决这个问题。MVC绑定触发验证错误
读取SO和interwebs,原因似乎是模型绑定在视图模型属性具有值之前触发视图模型属性的验证错误。我不确定这是否属实,实际情况如何,但这听起来很合理。
而且我读过两种解决方法,这听起来有点哈克:在一个空的观点 1.使用ModelState.Clear在上inital页面加载控制器中的操作方法或 2. Initialiase视图模型属性模型构造函数。 (尚未确认此技术)
这两种技术听起来都像是解决方法。我宁愿了解正在发生的事情并适当地设计我的代码。
有没有人遇到过这个问题?如果是这样,你在做什么?
代码按要求。您可以在下面看到Property1上的Data Annotation验证,该验证在初始请求(即首页加载)时被触发。
控制器的操作方法(重构为简单起见):
public ActionResult Index([Bind(Include = Property1, Property2, Property3, vmclickedSearchButton)] IndexVM vm, string Submit)
{
bool searchButtonClicked = (Submit == "Search") ? true : false;
if (searchButtonClicked)
{
PopulateUIData(vm); // Fetch data from database and pass them to VM
if (ModelState.IsValid)
{
vm.clickedSearchButton = true; // Used in the vm to avoid logic execution duing initial requests
DoWork(vm);
}
}
return View(vm);
}
// Inital request
IndexVM newVM = new IndexVM();
PopulateUIData(newVM); // Fetch data from database and pass to VM
return View(newVM);
}
设计注: 理想我想sepate渲染和submiting逻辑到单独的动作的方法。 也就是说在[HttpGet] Index()操作方法内渲染,并在[HttpPost] Index()操作方法内提交。 但由于我在视图中使用ForMethod.Get,因为此方法用于搜索功能,所以我只能使用[HttpGet]索引操作方法。
视图模型(重构为简单起见):
public class IndexVM
{
// DropDownLists
public IEnumerable<SelectListItem> DDLForProperty1 { get; set; }
public IEnumerable<SelectListItem> DDLForProperty2 { get; set; }
public IEnumerable<SelectListItem> DDLForProperty3 { get; set; }
[Required]
public int? Property1 { get; set; }
public int? Property2 { get; set; }
public int? Property3 { get; set; }
public bool vmclickedSearchButton { get; set; }
}
注: 视图模型是非常简单的。它包含下拉列表,DDL的选定属性以及其中一个属性的验证规则。
添加一个构造函数来视图模型和初始化属性的解决方法:
public IndexVM()
{
this.Property1 = 0;
}
可以显示模型和操作方法吗? – RyanCJI
通过编辑发表:) –