How to create validation Message for fields in razor MVC using ViewModel
I want to create field validation in my razor view.
This is quite easy because the MVC will automatically generate the validationMessageFor for us.
As what I have read that, we are not allowed to make the validation in model class, instead use viewModel class. Now I am little confuse on how to put the [Required] annotations in my viewModel.
My problem
This is how the real validation will work
View
<tr>
<td>Name</td>
<td colspan="2">
@Html.EditorFor(model => model.reg.registerNm, new { @class = "TextBoxAsLabel" })
@Html.ValidationMessageFor(model => model.reg.registerNm)
</td>
</tr>
ViewModel
public class reg
{
[Required]
public string registerNm {get;set;}
}
This will work, but in my ViewModel , the structure is different because I need to use data from many table
My ViewModel
public class RegisterInfoPA
{
public register reg { get; set; }
public personalInfo pinfo { get; set; }
public IEnumerable<maritalInfo> minfo { get; set; }
public IEnumerable<eduInfo> edInfo { get; set; }
public IEnumerable<monthlyIncome> monthlyIncomeInfo { get; set; }
public refundBank refundBK { get; set; }
public IEnumerable<accType> accType { get; set; }
}
so whenever I assign [Required] annotations here, the validation will not working.
How can I do validation message for my ViewModel, because my viewModel is using
public register reg { get; set; }
which represent so many fields, which I want, but for validation purpose, I only want one or two certain field to be validated.How can I achieve that?
Thank you .
链接地址: http://www.djcxy.com/p/56614.html上一篇: MVC 5内置验证消息,不会显示小数类型