data-val 属性未在 ASPNET MVC3 中使用 Html.ActionLink 创建的部分视图中生成
我有这样的结构
View
Html.RenderPartial (1)
Html.RenderPartial (2)
然后在我拥有的每个部分视图中
@Ajax.ActionLink("Edit", "EditMethod", null, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divEditContent"
}, new { @class = "my-edit" })
,当单击它时,它会渲染另一个部分,用类似以下内容的div“divEditContent”替换:
public PartialViewResult EditContactInformation()
{
return PartialView("_MyEdit", GetDetails());
}
_MyEdit部分有类似的内容:
@using (Ajax.BeginForm("SaveContactInformation", "MyBsol", new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divContactInformationContent"
}))
{
<script type="text/javascript">
$(function () {
$('form#ajaxForm').find('a.submit-link').click(function () {
$('form#ajaxForm').submit();
});
})
</script>
<a href="#" class="submit-link">Save</a>
@Ajax.ActionLink("Cancel", "CancelEdit", null, new AjaxOptions
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divContent"
}, new { @class = "my-cancel" })
@Html.TextBoxFor(model => model.Title, new { @class = "txt-input", placeholder = "Eg. Mr, Mrs, Ms" })
@Html.ValidationMessageFor(model => model.Title)
}
问题是data-val属性没有渲染对于输入,但是如果我将最后一个部分 (_MyEdit) 放在 Ajax.ActionLink (1) 的位置,它就会渲染它们。
这是一个错误吗?有什么解决办法吗? 提前致谢! Guillermo
更新
我发现了一些非常有趣的东西,如果我去(在带有 Firebug 的 Chrome 或 Firefox 中)并单击“查看源代码”,我看不到 data-val 属性,但如果我单击“检查元素”我看到他们了……奇怪……
I've this structure
View
Html.RenderPartial (1)
Html.RenderPartial (2)
Then in each one of those Partial Views I have
@Ajax.ActionLink("Edit", "EditMethod", null, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divEditContent"
}, new { @class = "my-edit" })
And when clicked it renders another partial,replacing the div "divEditContent" with something like:
public PartialViewResult EditContactInformation()
{
return PartialView("_MyEdit", GetDetails());
}
_MyEdit partial has something like:
@using (Ajax.BeginForm("SaveContactInformation", "MyBsol", new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divContactInformationContent"
}))
{
<script type="text/javascript">
$(function () {
$('form#ajaxForm').find('a.submit-link').click(function () {
$('form#ajaxForm').submit();
});
})
</script>
<a href="#" class="submit-link">Save</a>
@Ajax.ActionLink("Cancel", "CancelEdit", null, new AjaxOptions
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divContent"
}, new { @class = "my-cancel" })
@Html.TextBoxFor(model => model.Title, new { @class = "txt-input", placeholder = "Eg. Mr, Mrs, Ms" })
@Html.ValidationMessageFor(model => model.Title)
}
The problem is that data-val atttributes are not rendered for the input, BUT if I put the last partial (_MyEdit) where the Ajax.ActionLink (1) it DOES render them.
Is this a bug? Any workaround?
Thanks in advance! Guillermo
UPDATE
I've found something really interesting, if I go (in Chrome or Firefox with Firebug) and click on View Source I don't see data-val attributes, but if I click on Inspect Element I see them..weird...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望数据验证标记存在,则需要位于
FormContext
中。因此,如果您要动态生成表单的某些部分,则需要在部分视图中包含以下行:然后,您需要确保每次添加/删除项目时动态重新绑定不显眼的验证:
If you want the data validation tags to be there, you need to be in a
FormContext
. Hence, if you're dynamically generating parts of your form, you need to include the following line in your partial view:You then need to make sure you dynamically rebind your unobtrusive validation each time you add/remove items: