0

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)

countryroadscat
  • 1,660
  • 4
  • 26
  • 49
  • Hard to make any sense of your code and you need to post you models. If your trying to add dynamic content, then [this answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) may help –  May 02 '15 at 11:20
  • @StephenMuecke thank you for response. I updated question to be more precise. – countryroadscat May 02 '15 at 11:26
  • 1
    Look at the link I gave you to understand how to dynamically add the html necessary to post back to a collection (you can also look at using the `BeginCollectionItemHelper`) The key is to correctly name you controls with indexers. –  May 02 '15 at 11:30
  • @StephenMuecke still dont understand how it works. however i rebuild my question and my idea. Maybe u will be able to answer for this one? – countryroadscat May 02 '15 at 12:33
  • What is it that you think your script is doing? It seems to be creating some ` –  May 02 '15 at 12:43
  • exacly. each row contains a label with string (text of Question), button to remove a row, and other cells are labels with possible answers for that question. – countryroadscat May 02 '15 at 12:54
  • ` –  May 02 '15 at 13:00
  • This is a View to Create new Questionnaire: with questions and possible answers for them. So my intend was to create a model of Questionnaire in this View, and then send it to Controller. View is displaying only current 1 questionnare (current model). This is like administator page, user cant answer for question here. Just creating new Questionnaire. Ok so let say i will change label for Input text form. What next? still, how to create list and put it inside Questionnaire object – countryroadscat May 02 '15 at 13:04
  • 1
    Then you need to study the link I gave you in my first comment. You need to generate inputs that are correctly named. You have not shown your model for `Question` but assuming it contains a property `string Title`, then your inputs would be ` –  May 02 '15 at 13:09
  • I will try it then and give you a feedback. thank you anyway – countryroadscat May 02 '15 at 13:12
  • @StephenMuecke Works as hell! Thank you again! Add your comment as answer. I will accept it – countryroadscat May 02 '15 at 16:59
  • @StephenMuecke One more question. What if i will remove one row? for example i have got row 1 2 and 3. I remove row 2. So i have got 1 and 3 row left. However controls in these rows are named like Question[1].Title and Question[3].Title what leads me to retrive only First row in my model (3 is gone becouse there is no 2nd one, so how it could be 3rd:) ) . How to handle it? – countryroadscat May 03 '15 at 00:14
  • By default collection indexers must start at zero and be consecutive. But you can overcome this by adding a hidden input for an `Index` with a value equal to the indexer. Again study the link I gave you (in your case it might look like `` where `#` equals the indexer) –  May 03 '15 at 00:19

0 Answers0