简体   繁体   English

在ASP.NET MVC2中应用数据注释时出现问题

[英]Problem applying data annotation in asp.net mvc2

Im facing problem when trying to apply data annotation. 我在尝试应用数据注释时面临问题。 In my case im passing FormCollection in controller 就我而言,我在控制器中传递了FormCollection

[HttpPost]         
public ActionResult Create(string Button, FormCollection collection)
{
if (ModelState.IsValid)
      {
      }
else
      {
      }
}

and in ModelState.IsValid condition always have true value. 并且在ModelState.IsValid条件下始终具有真实值。 Although i have left some blank fields in View. 虽然我在视图中留下了一些空白字段。 Also EnableClientValidation() is also applied in View for client side validation but its not working. 此外,EnableClientValidation()也已在View中应用以进行客户端验证,但无法正常工作。 what may be the problem 可能是什么问题

Your view must be strongly typed, and the parameters of your Create function must contain an object to hold your Model, not a generic FormCollection. 您的视图必须是强类型的,并且Create函数的参数必须包含一个保存模型的对象,而不是通用FormCollection。 So if you have a model of name MyClass that you have annotated, then you should have that as the parameter. 因此,如果您已经注释了名称为MyClass的模型,则应将其作为参数。 Otherwise, how will the model binder know what class should it check your form against? 否则,模型绑定器将如何知道应针对哪个类检查表单?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM