我正在使用Razor引擎实现基于MVC的网站。
我已经在模型中创建了验证。 即学生验证是这样的:
[MetadataType(typeof(Student.StudentMetaData))]
public partial class Student
{
internal sealed class StudentMetaData
{
[Required(ErrorMessageResourceName = "FirstNameRequired", ErrorMessageResourceType = typeof(StudentMessages))]
public object FirstName { set; get; }
[Required(ErrorMessageResourceName = "FatherNameRequired", ErrorMessageResourceType = typeof(StudentMessages))]
public object FatherName { set; get; }
}
}
学生已经实现了IDataErrorInfo接口。数据字段存在于另一个部分类(由T4文件创建的部分类)中。
问题是当我在Razor
视图中使用它与客户端验证时使用:
@Html.TextBoxFor(m => m.FatherName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FatherName)
它没有获取我在资源文件中指定的消息。
我在使用WPF之前尝试过相同的技术,它的工作正常。
我允许在Web.config中进行客户端验证,并在我的视图中使用它:
@{
HtmlHelper.ClientValidationEnabled = true;
}
答案 0 :(得分:0)
Add Name space in modal class in mvc.
using System.ComponentModel.DataAnnotations;
public class TelePhones
{
[Required (ErrorMessage = "MobileNo is required")]
public long MobileNo { get; set; }
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
[DataType(DataType.Date)]
public string DateOfBirth { get; set; }
[Range(15, 70, ErrorMessage = "Price must be between 15 and 70")]
public int Age { get; set; }
}
client side validation in mvc
<div class="editor-label">
@Html.LabelFor(model => model.MobileNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MobileNo)
@Html.ValidationMessageFor(model => model.MobileNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DateOfBirth)
@Html.ValidationMessageFor(model => model.DateOfBirth)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>