问题
Here's my original create page (no nesting) - Client validation works
@model TennisClub.ViewModels.ClubMember.EditorModel
@{
    ViewBag.Title = "New Club Member";
    ViewBag.LegendTitle = "Club Member";
}
<h2>@ViewBag.Title</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Errors were found on the form. Please correct all errors and try again.")
    <fieldset>
        <legend>@ViewBag.LegendTitle</legend>
        @Html.EditorForModel()
        <p><input type="submit" value="Create" /></p>
    </fieldset>
}
<div>@Html.ActionLink("Back to List", "Index")</div>
Here's my new Create page (nested) - Client Validation FAILS
@model TennisClub.ViewModels.ClubMember.EditorModel
@{
    Layout = "~/Views/Shared/StandardLayouts/Create.cshtml";
    ViewBag.Title = "New Club Member";
    ViewBag.LegendTitle = "Club Member";
}
@Html.EditorForModel()
@if (ViewBag.CanUserAccessRestrictedContent)
Here's the layout (StandardLayouts/Create.cshtml) used by the above page
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Errors were found on the form. Please correct all errors and try again.")
    <fieldset>
        <legend>@ViewBag.LegendTitle</legend>
        @RenderBody()
        <p><input type="submit" value="Create" /></p>
    </fieldset>
}
<div>@Html.ActionLink("Back to List", "Index")</div>
Discussion
As far as I can tell, everything works fine using the nested approach except for client validation. When I look at the page source, the script references are there (validate and validate.unobtrusive), but no validation attributes are showing up in the html. If I don't use nested layouts, both the script references and the validation attributes are there.
I get the same results whether I use the standard attribute-based validation or FluentValidation.
Questions
- Is there something incorrect about the way I'm doing the layout nesting? It seems to be working fine except for this one issue, but maybe I'm doing things in a non-standard way. 
- Is there a setting in web.config or somewhere else I need to change to get client validation to work for pages that are nested more than one level deep? 
- Is this a bug in ASP.NET MVC that I should report to Microsoft? 
回答1:
try this for starters: In each view at the top you need to make sure you have a form context available -
@{
if(ViewContext.FormContext == null) {ViewContext.FormContext = new FormContext();
}
I actually put this in my _ViewStart.cshtml because ajax loaded views will require this for validation to work properly at times (plus some other code) - but give it a try for your issue
I believe your issue is if a view does not have a Ajax.BeginForm or an Html.BeginForm in the view itself - then the helpers will not emit the data-val attributes.
来源:https://stackoverflow.com/questions/5906116/nested-razor-layouts-causing-client-validation-to-fail