Nested BeginCollectionItem

前端 未结 3 1110
滥情空心
滥情空心 2020-12-16 16:39

I\'m using Steve Sanderson\'s BeginCollectionItem approach to add dynamic content. Everything works fine when I\'m doing it on the first level. However, when try to implemen

3条回答
  •  暖寄归人
    2020-12-16 17:09

    You can use the code from JonK answer but it will not work for dynamically added partials from ajax calls because "ViewData.TemplateInfo.HtmlFieldPrefix" comes empty.

    The solution is to add a property to your model with the form prefix and fill it in on the action called by the ajax function.

    To fetch the form prefix using jQuery you can simply do:

    // "this" a button that will trigger the ajax call and is within the div with the class "partial-enclosing-class"
    var parentRow = $(this).parents('.partial-enclosing-class');
    var hiddenInput = parentRow.find('input[name$="].Id"]');
    var formPrefix = hiddenInput.prop('name').replace(".Id", "");
    

提交回复
热议问题