9

我正在创建一个基于jQuery Mobile (Alpha 3)ASP.NET MVC 3应用程序,利用 MVC3 附带的不显眼的验证。当直接访问页面时(Url 中没有哈希),验证工作完美。但是,当您导航到页面时,jQuery Mobile 使用 Ajax Navigation 动态加载它(在 Url 中显示哈希)并且验证停止工作。

以下是使用中的代码示例:

模型:

[Required(ErrorMessage = "Missing value")]
[DisplayName("Property Display Name")]
public int? PropertyName { get; set; }

查看(剃刀):

@Html.LabelFor(model => model.PropertyName)
@Html.TextBoxFor(model => model.PropertyName)
@Html.ValidationMessageFor(model => model.PropertyName)

生成的 HTML:

<label for="PropertyName">Property Display Name</label>
<input data-val="true" data-val-number="The field Property Display Name must be a number." data-val-required="Missing value" id="PropertyName" name="PropertyName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="PropertyName" data-valmsg-replace="true"></span>

可能之前已经加载了其他页面并且 HTML 元素不再具有唯一 ID。除了滚动我自己的 Html Helper 类来为 Label、TextBox 和 ValidationMessage 生成 HTML 之外,还有什么方法可以处理这种情况吗?

4

3 回答 3

14

我一直在为同样的问题苦苦挣扎,但@Zote 为我指明了正确的方向。

parse()是要走的路,但请确保传入一个选择器,即:

jQuery.validator.unobtrusive.parse("form")

或者

jQuery.validator.unobtrusive.parse(document)

连接它的最好方法可能是通过 JQMspageshow事件。这将在每个新页面转换后触发,就像这样,您可能还希望在 jqm 完成它之前通过使用pagebeforeshow事件在页面上完成它的魔法

$('div').live('pageshow',function(event){
  jQuery.validator.unobtrusive.parse(".ui-page-active form");
});

通过使用.ui-page-active,您可以将搜索范围缩小到当前活动页面。

于 2011-02-19T20:36:44.807 回答
5

jQuery.validator.unobtrusive.parse()在加载新内容后打电话吗?在 Brad Wilson 的博客上阅读这篇文章。

于 2011-02-18T17:43:07.203 回答
0

我解决了我遇到的同样的问题,我的答案贴在这里 -

使用带有asp.net mvc2的jquery mobile时出现哈希导航问题

于 2011-09-20T03:38:37.403 回答