求指点,Razor和Entity Framewrok 怎么使用MVC集成的jquery.validate
求指点,Razor和Entity Framewrok 怎么使用MVC集成的jquery.validate
就是那种不需要另外添加Model的,直接使用Entity Framework中的类来执行前台和后台的数据Validate验证。谢谢。
------解决方案--------------------
建议看下mvc3源代码 就可以了解它的原理
还可以看下大牛 MVC系列知识
------解决方案--------------------
前台:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>DataEntities</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</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>
<div class="editor-label">
@Html.LabelFor(model => model.imageUrl)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.imageUrl)
@Html.ValidationMessageFor(model => model.imageUrl)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
entityframework创建的都是部分类,你可以自己再定义一个部分类例如:
后台:
public partial class DataEntities
{
[Required(ErrorMessage="必须")]
[StringLength(10,ErrorMessage="长度太长")]
[Range(10,20,ErrorMessage="测试")]
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string imageUrl { get; set; }
}