data-val 属性未在 ASPNET MVC3 中使用 Html.ActionLink 创建的部分视图中生成

发布于 2024-12-13 02:29:29 字数 1946 浏览 1 评论 0原文

我有这样的结构

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2024-12-20 02:29:29

如果您希望数据验证标记存在,则需要位于 FormContext 中。因此,如果您要动态生成表单的某些部分,则需要在部分视图中包含以下行:

@{ if(ViewContext.FormContext == null) {ViewContext.FormContext = new FormContext(); }

然后,您需要确保每次添加/删除项目时动态重新绑定不显眼的验证:

$("#form").removeData("validator");
$("#form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("#form");

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:

@{ if(ViewContext.FormContext == null) {ViewContext.FormContext = new FormContext(); }

You then need to make sure you dynamically rebind your unobtrusive validation each time you add/remove items:

$("#form").removeData("validator");
$("#form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("#form");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文