我使用jQuery.load()渲染部分视图.这部分看起来像这样:
$('#sizeAddHolder').load( '/MyController/MyAction',function () { ... });
我的控制器中的操作代码如下:
public ActionResult MyAction(byte id) { var model = new MyModel { ObjectProp1 = "Some text" }; return View(model); } [HttpPost] public ActionResult MyAction(byte id,FormCollection form) { // TODO: DB insert logic goes here var result = ...; return Json(result); }
我返回的部分视图看起来像这样:
<% using (Html.BeginForm("MyAction","MyController")) {%> <%= Html.ValidationSummary(true) %> <h3>Create my object</h3> <fieldset> <legend>Fields</legend> <div class="editor-label"> <%= Html.LabelFor(model => model.ObjectProp1) %> </div> <div class="editor-field"> <%= Html.TextBoxFor(model => model.Size.ObjectProp1) %> <%= Html.ValidationMessageFor(model => model.ObjectProp1) %> </div> div class="editor-label"> <%= Html.LabelFor(model => model.ObjectProp2) %> </div> <div class="editor-field"> <%= Html.TextBoxFor(model => model.ObjectProp2) %> <%= Html.ValidationMessageFor(model => model.ObjectProp2) %> </div> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %>
在这种情况下,客户端验证不起作用.包含验证消息的脚本还不包括在返回的视图中.我的模型类中的两个属性都有required和StringLength属性.
有没有办法在已经加载的视图中触发客户端验证?