MVC 3剃须刀@ Html.ValidationMessageFor不是局部加载通过jquery.load工作() [英] MVC 3 Razor @Html.ValidationMessageFor not working in partial loaded via jquery.load()

查看:208
本文介绍了MVC 3剃须刀@ Html.ValidationMessageFor不是局部加载通过jquery.load工作()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里把一个小例子只是复制的问题。
我有一个强类型的局部视图_Name.cshtml:

I have put together a small example here just to replicate the problem. I have a strongly typed partial view _Name.cshtml:

@model ValidationInPartial.ViewModels.MyViewModel

<h2>@ViewBag.Message</h2>

    <fieldset>
        <legend>Name</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.MyName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.MyName)
            @Html.ValidationMessageFor(model => model.MyName)
        </div>

        <a href="#" id="reload">Reload Name</a>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<script type="text/javascript">
    $(document).ready(function () {
        $("#reload").click(function () {
            $("#divName").load("Home/NameReload");
        });
    });
</script>

这最初加载和主Index.cshtml内部显示

that is initially loaded and displayed inside the main Index.cshtml

<div id="divForm">
    @using (Html.BeginForm()) {

        <div id="divName">
            @Html.Partial("_Name")
        </div>
    }
</div>

本场MYNAME是必需的和验证通过必需属性在MyViewModel实施

The field MyName is required and validation is implemented through Required attribute in MyViewModel

namespace ValidationInPartial.ViewModels
{
    public class MyViewModel
    {
        [Required(ErrorMessage = "Please enter a Name.")]
        public string MyName { get; set; }
    }
}

页面后,第一次加载,如果单击Create按钮离开该字段为空验证消息请输入一个名称。显示字段旁边和领域本身变成粉红色,这是预期的行为。
现在,通过点击刷新名称链接,这使Ajax调用(jquery.load(...)),部分被重新装载,这里是控制器code:

After the page is loaded the first time, if you click the Create button leaving the field empty the validation message "Please enter a Name." shows beside the field and the field itself turns pink, which is the expected behaviour. Now by clicking the "Reload Name" link, which makes an ajax call (jquery.load(...)), the partial is reloaded, here is controller code:

public PartialViewResult NameReload()
{
    MyViewModel myViewModel = new MyViewModel();
    ViewBag.Message = "Name Reloaded";
    return PartialView("_Name", myViewModel);
}

这时候如果你点击创建按钮离开该字段为空验证消息没有出现在字段旁边,虽然场变成粉红色。
事实证明,重装部分时,@ Html.ValidationMessageFor不呈现验证消息的第一次。

This time if you click the Create button leaving the field empty the validation message does not appear beside the field, although the field turns pink. It turns out that when reloading the partial the @Html.ValidationMessageFor doesn't render the validation message as the first time.

下面是jQuery的文件,我用

Here is the jquery files I use

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

我不知道这是在路上的剃刀引擎渲染@ Html.ValidationMessageFor或者是一个问题与jQuery的错误?
任何想法,为什么出现这种情况?

I wonder if this is a bug in the way the Razor engine renders the @Html.ValidationMessageFor or is that a problem with jquery? Any idea why this happens?

我也看到某处Ajax调用失去了页面中的所有脚本,其实我要保持任何JavaScript code部分内,使他们可以呈现和再次使用。

I have also read somewhere that the ajax call looses all the scripts for the page, in fact I have to keep any javascript code inside the partial so that they can be rendered and used again.

在此期间,我发现一个解决办法是在部分手动呈现什么应该由@ Html.ValidationMessageFor呈现是:

In the meantime I found a workaround which is to manually render in the partial what was supposed to be rendered by @Html.ValidationMessageFor which is:

<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="MyName"></span>

不过这种解决方法意味着,如果我们改变验证的视图模型必需属性中的类型或只是验证消息,我们需要修改这个硬codeD片的HTML视图。

However this workaround means that if we change the type of validation or just the validation message inside the Required attribute in the ViewModel, we need to modify this hard-coded piece of html in the view.

推荐答案

@NickBork有一个伟大的答案在这里。关键是,ASP.NET的MVC渲染引擎不输出验证脚本,如果它不认为有一种形式。所举的例子是黑客把购买的形式,然后选择HTML的内部分从被返回,基本上抛形式的外包装了。

@NickBork has a great answer here. The key is that ASP.NET's MVC rendering engine does not output the validation script if it doesn't think that there is a form. The example given hacks it buy putting in a form and then selection an inner section of HTML from was was returned, essentially throwing the outer wrapper of the form away.

有另一种方法,这样你可以得到你的看法:

There is another method so that you can just get your view:

ViewContext.FormContext = new FormContext();

通过这种方法,但实际上不会FORM code输出,但验证标记将在那里。

With this method, there won't actually be FORM code output, but the validation markup will be there.

谢谢,
约翰·

Thanks, John

这篇关于MVC 3剃须刀@ Html.ValidationMessageFor不是局部加载通过jquery.load工作()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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