我想要客户端验证作为服务器端验证.我意识到如下:
型号:(该模型具有包含Test类的DataModel(dbml))
namespace MyProject.TestProject { [MetadataType(typeof(TestMetaData))] public partial class Test { } public class TestMetaData { [required(ErrorMessage="Please enter a name.")] [StringLength(50)] public string Name { get; set; } } }
控制器没什么特别的.
风景:
<% Html.EnableClientValidation(); %> <% using (Ajax.BeginForm("Index","Test",FormMethod.Post,new AjaxOptions {},new { enctype = "multipart/form-data" })) {%> <%= Html.AntiForgeryToken()%> <fieldset> <legend>Widget Omschrijving</legend> <div> <%= Html.LabelFor(Model => Model.Name) %> <%= Html.TextBoxFor(Model => Model.Name) %> <%= Html.ValidationMessageFor(Model => Model.Name) %> </div> </fieldset> <div> <input type="submit" value="Save" /> </div> <% } %>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script> <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
最终它必须工作,但它不工作100%:
按确认按钮后,无刷新页面.
它也做了“一半”客户端验证.只有当您在文本框中键入一些文本,然后退格键入的文本.客户端验证出现.但是当我尝试通过点击控件之间没有客户端验证.
我想念一些参考书吗? (我使用Asp.Net MVC 2 RTM)
解决方法
更改加载的JavaScript的顺序…
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
有完全相同的问题,这清除了我…