xVal 和 ViewModel 模式 - 可以做到吗?
我已经将 xVal 添加到 NerdDinner 应用程序中 - 到目前为止一切顺利,我在一行中使用 jQuery.validate 进行了客户端验证,这确实很漂亮。 但我似乎无法让 xVal 来验证复杂的对象。 假设我有一个看起来像这样的晚餐对象:
public class Dinner
{
[Required]
public string Title { get; set; }
}
和另一个对象,一个容器:
public class DinnerWrapper
{
public Dinner Dinner { get; set; }
public string Name { get; set; }
}
如果我的控制器将 Dinner
传递给视图,我可以让 xVal 在表单末尾执行客户端验证,像这样:
<% using (Html.BeginForm())
{ %>
<fieldset>
<p>
<label for="Title">
Dinner Title:</label>
<%= Html.TextBox("Title") %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
</fieldset>
<% } %>
<%=Html.ClientSideValidation<Dinner>()%>
但是当我传递 DiningWrapper 时,我无法让它工作 - xVal 不会使用以下设置执行客户端验证:
<% using (Html.BeginForm())
{ %>
<fieldset>
<p>
<label for="Title">
Dinner Title:</label>
<%= Html.TextBox("Title", Model.Dinner.Title) %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
</fieldset>
<% } %>
<%=Html.ClientSideValidation<DinnerWrapper>()%>
有什么想法吗? 到目前为止,我已经成功地将 xVal(和 NHaml)集成到 NerdDinner 应用程序中,但我似乎遇到了障碍。
I've been adding xVal to the NerdDinner app - so far so good, I get client-side validation with jQuery.validate in one line, which is truly beautiful. But I can't seem to get xVal to validate a complex object. Say I have a Dinner object that looks like this:
public class Dinner
{
[Required]
public string Title { get; set; }
}
and another object, a container:
public class DinnerWrapper
{
public Dinner Dinner { get; set; }
public string Name { get; set; }
}
If my controller passes Dinner
to the View, I can get xVal to perform client-side validation at the end of my form, like this:
<% using (Html.BeginForm())
{ %>
<fieldset>
<p>
<label for="Title">
Dinner Title:</label>
<%= Html.TextBox("Title") %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
</fieldset>
<% } %>
<%=Html.ClientSideValidation<Dinner>()%>
But I can't get it to work when I am passing DinnerWrapper - xVal doesn't perform client-side validation with the following setup:
<% using (Html.BeginForm())
{ %>
<fieldset>
<p>
<label for="Title">
Dinner Title:</label>
<%= Html.TextBox("Title", Model.Dinner.Title) %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
</fieldset>
<% } %>
<%=Html.ClientSideValidation<DinnerWrapper>()%>
Any ideas? So far I've successfully integrated xVal (and NHaml) into the NerdDinner app, but I seem to have hit a roadblock.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明我不必更改 ClientSideValidation 行 - 它的工作原理如下:
Turns out I didn't have to change the ClientSideValidation line - it works like this: