EditorFor未呈现数据验证属性,而不BeginForm [英] EditorFor not rendering data validation attributes without BeginForm

查看:101
本文介绍了EditorFor未呈现数据验证属性,而不BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC应用程序,我要动态(​​这就像PartialView在控制器端)

In MVC Application, I want to render some parts of form dynamically (which are like PartialView on Controller Side)

在局部视图,我没有Html.BeginForm()作为表单标签已经呈现。

In the partial view, i dont have Html.BeginForm() as the form tag is already rendered.

@model Introduction.Models.Human
<div>
    @Html.EditorFor(model => model.MarriageInformation.SpouseDetails)
    <div class="editor-label">
        @Html.LabelFor(model => model.MarriageInformation.DOM)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MarriageInformation.DOM)
        @Html.ValidationMessageFor(model => model.MarriageInformation.DOM)
    </div>
</div>

我现在面临的问题是在这种情况下EditorFor不返回所有的数据val- *属性。

Problem I am facing is in this case EditorFor does not return all the data-val-* attributes.

<div>
   <div class="editor-label">
    <label for="MarriageInformation_SpouseDetails_Name">Name</label>
</div>
<div class="editor-field"><input class="text-box single-line" id="MarriageInformation_SpouseDetails_Name" name="MarriageInformation.SpouseDetails.Name" type="text" value="" /> 

在设计这还是我米在这里缺少什么?是否有任何工作在这里?

Is this by design or I m missing anything here? Is there any work around here?

我想到的选项阿贾克斯负荷后 - 剥离的形式和内注入内容

The option i am thinking is after ajax load - strip the form and inject inner content.

推荐答案

你的假设,这是由设计正确。如果你查看​​你会看到以下内容:

you're correct in assuming that this is by design. if you check the source you'll see the following:

// Only render attributes if unobtrusive client-side validation is enabled, and then only if we've
// never rendered validation for a field with this name in this form. Also, if there's no form context,
// then we can't render the attributes (we'd have no <form> to attach them to).
public IDictionary<string, object> GetUnobtrusiveValidationAttributes(string name, ModelMetadata metadata)

要解决这个问题,我们可以写一个使用扩展方法在我们的局部视图:

To fix this we can write an extension method for use in our partial view:

public static class HtmlExtentions
{
    public static void EnablePartialViewValidation(this HtmlHelper helper)
    {
        if (helper.ViewContext.FormContext == null)
        {
            helper.ViewContext.FormContext = new FormContext();
        }
    }
}

,然后用它在我们的局部视图:

And then use it in our partial view:

@model Introduction.Models.Human
@{ Html.EnablePartialViewValidation(); }
<div>
    @Html.EditorFor(model => model.MarriageInformation.SpouseDetails)
    <div class="editor-label">
        @Html.LabelFor(model => model.MarriageInformation.DOM)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MarriageInformation.DOM)
        @Html.ValidationMessageFor(model => model.MarriageInformation.DOM)
    </div>
</div>

最后一步是处理解析在我们的AJAX回调新的验证属性:

The last step is to handle parsing the new validation attributes in our ajax callback:

$(function () {
    $('button').click(function (e) {
        e.preventDefault();
        $.get('@Url.Action("AddSpouse")', function (resp) { 
            var $form = $('form');
            $form.append(resp);                    
            $form.removeData("validator").removeData("unobtrusiveValidation");
            $.validator.u‌​nobtrusive.parse($form);
        })
    })
});

这篇关于EditorFor未呈现数据验证属性,而不BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆