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
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