将 MVC1/xVal 项目升级到 MVC2 并与客户端验证兼容
我目前正在此处寻找将项目从 MVC1 迁移到 MVC2 的方法。我们使用 xVal 进行客户端验证。
据我了解,我只需删除对 xVal 的引用并替换为 MVC2 EnableClientSideValidation 就可以了。还需要什么吗?
另外,由于 xVal 无法在客户端验证模型列表,MVC2 + ClientSideValidation 仍然是这种情况吗? (我的意思是“模型列表”是在 List
上进行验证)
示例是: 控制器:
public ActionResult Index()
{
Models.Model1 model = new Models.Model1();
model.Usernames = new List<Models.Model2>();
model.Usernames.Add(new Models.Model2 { });
model.Usernames.Add(new Models.Model2 { });
model.Usernames.Add(new Models.Model2 { });
model.Usernames.Add(new Models.Model2 { });
return View(model);
}
模型 1:
class Model1 {
public List<Model2> Usernames { get; set; }
}
模型 2:
class Model2 {
[Required]
public string Username { get; set; }
}
视图:
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) { %>
<% foreach (var username in Model.Usernames) { %>
<% Html.RenderPartial("View2", username); %>
<br /><br />
<% } %>
<input type="submit" />
<% } %>
视图 2:
<%= Html.EditorFor(f => f.Username) %>
<%= Html.ValidationMessageFor(a => a.Username)%>
这是一个 POC,其中 MVC2 客户端验证失败,因为它通过字段 id 进行验证...
I am currently looking here to migrate a project from MVC1 to MVC2. We are using xVal for client side validation.
From what I understand, I only have to remove references to xVal and replace by MVC2 EnableClientSideValidation and it should do the trick. Anything else would be required ?
Also, with xVal could not validate on client side a list of models, is it still the case with MVC2 + ClientSideValidation ? (what I mean with the "list of models" is doing validation on a List<TypeOfModel>
)
Example of this is :
controller:
public ActionResult Index()
{
Models.Model1 model = new Models.Model1();
model.Usernames = new List<Models.Model2>();
model.Usernames.Add(new Models.Model2 { });
model.Usernames.Add(new Models.Model2 { });
model.Usernames.Add(new Models.Model2 { });
model.Usernames.Add(new Models.Model2 { });
return View(model);
}
Model1:
class Model1 {
public List<Model2> Usernames { get; set; }
}
Model2:
class Model2 {
[Required]
public string Username { get; set; }
}
View:
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) { %>
<% foreach (var username in Model.Usernames) { %>
<% Html.RenderPartial("View2", username); %>
<br /><br />
<% } %>
<input type="submit" />
<% } %>
View2:
<%= Html.EditorFor(f => f.Username) %>
<%= Html.ValidationMessageFor(a => a.Username)%>
This is a POC where MVC2 client side validation is failing since it validates by field id...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
去过那里,完成了,拿到了 T 恤。希望我从来没有去过那里——坦率地说,一旦正确实现了 xVal,MVC2 验证就无法与 xVal 相提并论。此外,MVC3 验证做得非常非常好。因此,如果我要放弃 xVal,我会转向让它与 MVC3 一起工作,这是一个非常不同的野兽。
Been there, done that, got the T-Shirt. And wish I never went there -- MVC2 validation is frankly not on par with xVal once you've got xVal properly implemented. In addition, MVC3 validation is much, much better done. So, if I were jumping off xVal, I would vector towards making it work with MVC3 which is a much different beast.
虽然我有点赞同 Wyatt 在这里所说的,尤其是关于 MVC3 的部分,但我一直在使用 MVC2 客户端验证,而且这并不是一次糟糕的体验。
要回答您的问题,您需要执行以下操作:
1. 在您的视图中包含 MicrosoftAjax.js 和 MicrosoftMvcValidation.js。
2. 然后只需调用帮助器 <% Html.EnableClientValidation(); %>
它应该拾取您的 DataAnnotation 模型验证规则,并毫不费力地为您提供客户端验证。
Though I subscribe somewhat to what Wyatt said here especially the part about MVC3, but I have been using MVC2 client side validation and it has not been a bad experience at all.
To answer your question, you need to do the following:
1. Include MicrosoftAjax.js and MicrosoftMvcValidation.js in your view.
2. Then simply call the helper <% Html.EnableClientValidation(); %>
It should pickup your DataAnnotation model validation rules and give you client side validation without much effort.