0

I'm trying to add a button to 'add new row' to the table in my mvc view.

First I tried this and it worked.

<script type="text/javascript">
$(document).ready(function () {
function addRow() {
var html = '<tr><td>' +
'<input type="text" id="Text1">' +
'</td><td>' +
'<input type="text" id="Text2">' +
'</td></tr>'
$(html).appendTo($("#table1 > tbody"))
};
$(".BtnAdd").click(addRow);
});
</script>

Then I had to change input type="text" to @Html.TextAreaFor()

So I tried this...but not working

<script type="text/javascript">
$(document).ready(function () {
function addRow() {
var html = '<tr><td>' +
'@Html.TextAreaFor(m => m.schedule1)' +
'</td><td>' +
'@Html.TextAreaFor(m => m.Schedule2)' +
'</td></tr>' +
$(html).appendTo($("#table1 > tbody"))
};
$(".BtnAdd").click(addRow);
});
</script>

Anyone please help me...Sorry if my way of presenting the problem is bad (new to .net)

Is there any other good way? (using for each loop etc)

Thanks

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Ashin
  • 139
  • 3
  • 15
  • 1
    you are mixing client side code with server, you are not clear about client side and server side – Ehsan Sajjad May 19 '15 at 10:48
  • How can i go forward? – Ashin May 19 '15 at 10:49
  • first clear your mind about what is server side code and what is client side code – Ehsan Sajjad May 19 '15 at 10:50
  • Easiest solution is to use the [BeginCollectionItem](https://www.nuget.org/packages/BeginCollectionItem/) helper (refer [example here](http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/)) or for a pure client side example refer [this answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) –  May 19 '15 at 10:51

0 Answers0