Is it possible to provide dropdownlist in the new row added to WebGrid? I tried adding this
'<td >@Html.DropDownList("Team", (IEnumerable<SelectListItem>)ViewBag.Teams, "")</td>'
and it says
Uncaught SyntaxError: Invalid or unexpected token at
<td ><select id="Team" name="Team"><option value=""></option>
.
Code:
var row = '<tr class="webgrid-row-style">'+
'<td class="col3Width"><input type="text" id="Date" value="" class="edit-mode date-picker" /></td>' +
'<td >@Html.DropDownList("Team", (IEnumerable<SelectListItem>)ViewBag.Teams, "")</td>' +
'<td ><span><input type="text" id="Name" value="" class="edit-mode /></span></td>' +
'<td ><span><input type="text" id="Category" value="" class="edit-mode/></span></td>' +
'<td ><span><input type="text" id="Received_Count" value="" class="edit-mode" /></span></td>' +
'<td ><span><input type="text" id="Done_Count" value="" class="edit-mode" /></span></td>' +
'<td ><button class="add-item edit-mode">Add</button> <button class="remove-item edit-mode">Cancel</button></td>' +
'</tr>';
$('table tbody:last').append(row);
Error:
When you use
@Html.DropDownList
in a javascript string likeit generates a string in multiple lines like below
which is invalid, hence you got the error.
As a workaround, try to place the
@Html.DropDownList
inside a hidden div somewhere in your viewthen get the generated string using
$('#divTeams').html()