EditorFor not rendering data validation attributes without BeginForm

前端 未结 2 1711
醉话见心
醉话见心 2021-01-17 09:16

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

In the partial view, i dont have Html.BeginForm()

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 10:00

    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");
    

提交回复
热议问题