repeat the summernote editor for all the textareas in same page

戏子无情 提交于 2019-12-11 16:18:10

问题


I have a cshtml viewpage like below

    <div class="form-group">

            @foreach (var fields in Model.ListProductFields)
            {

               @Html.LabelFor(x => x.ProductFieldNameEn, fields.FieldName, htmlAttributes: new { id="", @class = "control-label col-md-2" })

               <div class="col-md-10">

                  @Html.TextAreaFor(model => model.FieldValue, new { htmlAttributes = new { @class = "form-control", @row = 5 } })

               </div>                    
            }

    </div>

using above div section I can populate multiple textareas with its relavant label

this is how it shows when this is working

Then I try to add summernote rich text editor for all of these text areas , using following home-index.js file

(function ($) {
    function HomeIndex() {
        var $this = this;

        function initialize() {
            $('#FieldValue').summernote({
                focus: true,
                height: 150,
                codemirror: {
                    theme: 'united'
                }
            });
        }

        $this.init = function () {
            initialize();
        }
    }

    $(function () {
        var self = new HomeIndex();
        self.init();
    })
}(jQuery))

then its apply for first text area. not apply for rest of the text areas

like below


回答1:


Your creating invalid html because each textarea has the same id attribute and ('#FieldValue') will only ever return the first element with id="FieldValue".

In addition this will not bind to your model when you submit the form. Instead, use a for loop to generate the html and give the textarea a class name for use as a jQuery selector (note that property ListProductField will need to implement IList or alternatively you can use a custom EditorTemplate for the type)

@for (int i = 0; i < Model.ListProductFields.Count; i++)
{
    @Html.LabelFor(x => x.ListProductFields[i].ProductFieldNameEn, Model.ListProductFields[i].FieldName, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextAreaFor(m => m.ListProductFields[i].FieldValue, new { htmlAttributes = new { @class = "form-control summernote", @row = 5 } })
    </div>                    
}

and change the selector to

$('.summernote').summernote({

Side note: It appears your @Html.LabelFor() is incorrect. It is creating a label associated with property ProductFieldNameEn but you control is for property FieldName. I think you have the parameters the wrong wa around and it should be

@Html.LabelFor(x => x.ListProductFields[i].FieldName, Model.ListProductFields[i].ProductFieldNameEn, ...

which will associated the label with the following textarea, and display the value of property ProductFieldNameEn




回答2:


     <div class="form-group">

                @foreach (var fields in Model.ListProductFields)
                {

                   @Html.LabelFor(x => x.ProductFieldNameEn, fields.FieldName, htmlAttributes: new { id="", @class = "control-label col-md-2" })

                   <div class="col-md-10">

                      @Html.TextAreaFor(model => model.FieldValue, new { htmlAttributes = new { @class = "form-control  mySummerNote", @row = 5 } })

                   </div>                    
                }
    </div>


(function ($) {
    function HomeIndex() {
        var $this = this;

        function initialize() {
            $('.mySummerNote').summernote({
                focus: true,
                height: 150,
                codemirror: {
                    theme: 'united'
                }
            });
        }

        $this.init = function () {
            initialize();
        }
    }

    $(function () {
        var self = new HomeIndex();
        self.init();
    })
}(jQuery))


来源:https://stackoverflow.com/questions/33664796/repeat-the-summernote-editor-for-all-the-textareas-in-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!