Question rebuild.
I have a html table:
<div class="form-group">
@Html.Label("Lista pytan:", new { @class = "col-md-2 control-label" })
<table id="questionTable" border="0">
<tbody id="questionTableBody">
</tbody>
</table>
</div>
Which is builded dynamically via javascript AddRow() function called onclick:
function addRow(table, value)
{
var tab = document.getElementById(table);
var rowCount = tab.rows.length;
var row = tab.insertRow(rowCount);
var cell1 = row.insertCell(0);
var lp = document.createElement("label");
lp.name = "lblLp[]";
lp.className = "col-md-2 control-label";
lp.textContent = "" + (rowCount + 1);
cell1.appendChild(lp);
var cell2 = row.insertCell(1);
var element1 = document.createElement("label");
element1.name = "lbl[]";
element1.className = "col-md-2 control-label";
element1.textContent = document.getElementById(value).value;
cell2.appendChild(element1);
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "button";
element2.name = "btn[]";
element2.value = "Usun";
element2.className = "btn btn-default";
element2.addEventListener('click', function () {
deleteRow(table, row.rowIndex);
});
cell3.appendChild(element2);
}
Based on this table i want to create a model right before calling
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add(AddQuestionnaireModel model)
in my controller.
My model looks like this:
public class AddQuestionnaireModel
{
public QuestionnaireServiceReference.Questionnaire Questionnaire {get;set;}
public AddQuestionnaireModel()
{
Questionnaire = new QuestionnaireServiceReference.Questionnaire();
}
}
And Questionnaire is an object generated form EntityFramework:
public partial class Questionnaire
{
public Questionnaire()
{
this.Question = new HashSet<Question>();
}
public long questionnaire_id { get; set; }
public string name { get; set; }
public virtual ICollection<Question> Question { get; set; }
}
Most important thing is that Questionnaire contain list of objects. How to via Ajax or Javascript fill a Model in my View?
I have add onclick funkction to my button submit button, named: fillQuestionnaire().
Can you help me build my fillQuestionnaire() javascript funkction? (or ajax if u have other ideas)