ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout

前端 未结 5 1627
一个人的身影
一个人的身影 2020-12-09 13:31

I just realized that when I place a form tag on my layout page, surrounding the RenderBody section, the unobtrusive validation is not being generated. Something like this:

5条回答
  •  佛祖请我去吃肉
    2020-12-09 14:02

    You could apply a grotesque hack inside your view:

    @{
        var originalContext = ViewContext.FormContext;
        ViewContext.FormContext = new FormContext();
    }
    
    
    @Html.TextBoxFor(x => x.Prop1)
    @Html.ValidationMessageFor(x => x.Prop1)
    
    @Html.TextBoxFor(x => x.Prop2)
    @Html.ValidationMessageFor(x => x.Prop2)
    
    @{
        ViewContext.FormContext = originalContext;
    }
    

提交回复
热议问题