Get 方法上的数据注释触发验证
Data annotation trigger validation on Get method
我有这个视图模型
public class ProductViewModel : BaseViewModel
{
public ProductViewModel()
{
Categories = new List<Categorie>
}
[Required(ErrorMessage = "*")]
public int Code{ get; set; }
[Required(ErrorMessage = "*")]
public string Description{ get; set; }
[Required(ErrorMessage = "*")]
public int CategorieId { get; set; }
public List<Categorie> Categories
}
我的控制器是这样的
[HttpGet]
public ActionResult Create(ProductViewModel model)
{
model.Categories = //method to populate the list
return View(model);
}
问题是,一旦显示视图,就会触发验证。
为什么会这样?
在此先感谢您的帮助。
更新
景色是这样的
@using (Html.BeginForm("Create", "Product", FormMethod.Post, new { @class = "form-horizontal", @role = "form" }))
{
<div class="form-group">
<label for="Code" class="col-sm-2 control-label">Code*</label>
<div class="col-sm-2">
@Html.TextBoxFor(x => x.Code, new { @class = "form-control"})
</div>
</div>
<div class="form-group">
<label for="Description" class="col-sm-2 control-label">Desc*</label>
<div class="col-sm-2">
@Html.TextBoxFor(x => x.Description, new { @class = "form-control", maxlength = "50" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Categorie*</label>
<div class="col-sm-4">
@Html.DropDownListFor(x => x.CategorieId, Model.Categories, "Choose...", new { @class = "form-control" })
</div>
</div>
您的 GET 方法有一个模型参数,这意味着 DefaultModelBinder
初始化模型实例并根据路由值设置其属性。由于您没有传递任何值,所有 属性 值都是 null
因为它们都具有 [Required]
属性,验证失败并添加了 ModelState
错误,这就是错误的原因首次呈现视图时显示。
您不应将模型用作 GET 方法中的参数。除了它创建的难看的查询字符串之外,所有复杂对象和集合的属性的绑定都会失败(看看你的查询字符串 - 它包括 &Categories=System.Collections.Generic.List<Categorie>
当然会失败和 属性 Categories
将是默认的空集合)。此外,您很容易超出查询字符串限制并引发异常。
如果您需要将值传递给 GET 方法,例如 Code
的值,那么您的方法应该是
[HttpGet]
public ActionResult Create(int code)
{
ProductViewModel model = new ProductViewModel
{
Code = code,
Categories = //method to populate the list
};
return View(model);
}
我有这个视图模型
public class ProductViewModel : BaseViewModel
{
public ProductViewModel()
{
Categories = new List<Categorie>
}
[Required(ErrorMessage = "*")]
public int Code{ get; set; }
[Required(ErrorMessage = "*")]
public string Description{ get; set; }
[Required(ErrorMessage = "*")]
public int CategorieId { get; set; }
public List<Categorie> Categories
}
我的控制器是这样的
[HttpGet]
public ActionResult Create(ProductViewModel model)
{
model.Categories = //method to populate the list
return View(model);
}
问题是,一旦显示视图,就会触发验证。
为什么会这样?
在此先感谢您的帮助。
更新
景色是这样的
@using (Html.BeginForm("Create", "Product", FormMethod.Post, new { @class = "form-horizontal", @role = "form" }))
{
<div class="form-group">
<label for="Code" class="col-sm-2 control-label">Code*</label>
<div class="col-sm-2">
@Html.TextBoxFor(x => x.Code, new { @class = "form-control"})
</div>
</div>
<div class="form-group">
<label for="Description" class="col-sm-2 control-label">Desc*</label>
<div class="col-sm-2">
@Html.TextBoxFor(x => x.Description, new { @class = "form-control", maxlength = "50" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Categorie*</label>
<div class="col-sm-4">
@Html.DropDownListFor(x => x.CategorieId, Model.Categories, "Choose...", new { @class = "form-control" })
</div>
</div>
您的 GET 方法有一个模型参数,这意味着 DefaultModelBinder
初始化模型实例并根据路由值设置其属性。由于您没有传递任何值,所有 属性 值都是 null
因为它们都具有 [Required]
属性,验证失败并添加了 ModelState
错误,这就是错误的原因首次呈现视图时显示。
您不应将模型用作 GET 方法中的参数。除了它创建的难看的查询字符串之外,所有复杂对象和集合的属性的绑定都会失败(看看你的查询字符串 - 它包括 &Categories=System.Collections.Generic.List<Categorie>
当然会失败和 属性 Categories
将是默认的空集合)。此外,您很容易超出查询字符串限制并引发异常。
如果您需要将值传递给 GET 方法,例如 Code
的值,那么您的方法应该是
[HttpGet]
public ActionResult Create(int code)
{
ProductViewModel model = new ProductViewModel
{
Code = code,
Categories = //method to populate the list
};
return View(model);
}