1

I want to add row to my table dynamicly like this :

 var tableRef = document.getElementById('tblData').getElementsByTagName('tbody')[0];
        var newRow = tableRef.insertRow(tableRef.rows.length);
        var newCell = newRow.insertCell(0);
        newCell.appendChild(document.createElement('input'));
        var newCell = newRow.insertCell(1);
        newCell.appendChild('@Html.DropDownList("AppVersionGroupId", string.Empty)');

but output is :

newCell.appendChild('<select id="AppVersionGroupId" name="AppVersionGroupId"><option value=""></option>
<option value="46a08053-56e3-4320-aec3-7b521cca28ab">op1</option>
<option value="36ff6b7a-bdef-4506-b771-95cc42cd2667">op2</option>
<option value="e84fcfc5-8a46-47fd-8584-c00a5642f246">op3</option>
</select>'); 

how can I use html.!!! in my javascript ?!

0

3 Answers 3

6

Thanks a lot guys . The answer that work for me was :

newCell.innerHTML = '@Ajax.JavaScriptStringEncode(Html.DropDownList("AppVersionGroupId", string.Empty).ToHtmlString())'

If anything is wrong let me know .

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

Comments

0

You need to use @Html.Raw and the innerHtml JavaScript function:

newCell.innerHtml = '@Html.Raw(Html.DropDownList("AppVersionGroupId", string.Empty)');

5 Comments

It dos not work : var newCell = newRow.insertCell(1); newCell.appendChild('<select id="AppVersionGroupId" name="AppVersionGroupId"><option value=""></option> <option value="46a08053-56e3-4320-aec3-7b521cca28ab">op1</option> <option value="36ff6b7a-bdef-4506-b771-95cc42cd2667">op2</option> <option value="e84fcfc5-8a46-47fd-8584-c00a5642f246">op3</option> </select>');
why output create in multiline ?!
@JavadAzimi, yup, my bad. innerHtml is not a function, but a property. I updated the answer.
thanks bro... problem : output break in multiline and made error
newCell2.innerHTML = '<select id="AppVersionGroupId" name="AppVersionGroupId"><option value=""></option> <option value="46a08053-56e3-4320-aec3-7b521cca28ab">باگ</option>...
0

appendChild expect a DOM node and not String, so what you need is a function that create DOM element from a given HTML string

function createElementFromHtml(html){ 
  var div = document.createElement('div');
  div.innerHTML = html;
  return div.childNodes;
};

after defining the above function in your main script, you can call it like this

var dropDown = createElementFromHtml('@Html.DropDownList("AppVersionGroupId", string.Empty)');
newCell.appendChild(dropDown); // <--- here DOMElement and not string

Hope this help!

References:

1 Comment

thanks a lot .. it work like @Andrei V solution . But problem is dropDown tags are in multiline and breaked . it should be in single line and between two "'"

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.