Mis*_*pic 9 asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
有点神秘.我有一个带有Year属性的viewmodel:
public class TradeSpendingSalesViewModel
{
public string ProductCode { get; set; }
public IEnumerable<SelectListItem> AllowTypeSelect { get; set; }
public string AllowType { get; set; }
public IEnumerable<SelectListItem> YearsSelect { get; set; }
public int Year { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我将一个空的viewmodel发布到我的控制器:
[HttpPost]
public ActionResult Index(TradeSpendingSalesViewModel vm)
{
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
foreach (var e in allErrors)
{
Response.Write(e.ErrorMessage);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我收到一个错误消息:"年份字段是必需的."
由于我没有使用Required属性注释viewmodel Year字段,因此我不清楚为什么会生成此错误.
有任何想法吗?
Dim*_*ima 16
默认情况下,ValueTypes隐式标记为Requiredmvc.实际上,这是为了目的,因为根据定义它们是不可为空的.
我建议你设置Year为int?,否则,如果你的情况不正确,你可能会改为false
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes
在Global.asax.cs中.
| 归档时间: |
|
| 查看次数: |
5211 次 |
| 最近记录: |