0

I have this function to add a row in a table and I need a drop-down list in the first cell with value that are on database. I tried with this: td1.innerHTML = "@Html.DropDownList("SubTipologia", lista)"; But does not work. Can you help me? Below there is the he function:

function aggiungiRiga(my_table) {
    var tbody = document.getElementById(my_table).getElementsByTagName("tbody")[0];
    var colonne = document.getElementById(my_table).getElementsByTagName('th');
    var row = document.createElement('tr');
    var td1 = document.createElement("td");
    td1.innerHTML = "<div>@Html.DropDownList("SubTipologia", lista)</div>";
    var td2 = document.createElement("td");
    td2.contentEditable = true;
    td2.innerHTML = "<input type=text placeholder='Volume'>";
    var td3 = document.createElement("td");
    td3.contentEditable = true;
    td3.innerHTML = "<input type = text placeholder = 'Volume al m&sup3'>";
    var td4 = document.createElement("td");
    td4.contentEditable = true;
    td4.innerHTML = "<input type=text placeholder='Valore Totale'>";
    var td5 = document.createElement("td");
    td5.innerHTML = "<button type=button class='btn btn -default btn - sm'><span class='glyphicon glyphicon-trash'></span></button >";
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    row.appendChild(td4);
    row.appendChild(td5);
    tbody.appendChild(row);
}
1
  • You are using MVC right? Commented Sep 24, 2018 at 6:09

1 Answer 1

0

As I can see, you try to run server-side code on client (browser).

Browser doesn't khow about Razor or Html helper. So you need to use HTML instead of @Html.DropDownList("SubTipologia", lista) like this:

td1.innerHTML = "<div><select id='SubTipologia'></select></div>";

And then you can add items to <select> e.g. like here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.