Click here to Skip to main content
14,364,529 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hope you all are fine I was working in mvc 5 and need to add a dropdown list which will get the values from model/Controller. The issue is when I add the @Html.DropdownlistFor function the code works but when I append a new row in the Table tag using javascript that Dropdown doesn't appears.

This is for the MVC 5 with entity framework(.net).

//This code works for me and appears a DropDownList.
<td>
                        @Html.DropDownList("TravelMode", null, htmlAttributes: new { @id = "travelmode", @class = "form-control t-mode01" })
 </td>

 //But this doesn't shows any dropdownlist.
 

    //For Travel Details
    $(document).on("click", ".classAdd", function () {

        var rowCount = $('.data-contact-person').length + 1;

        var name = document.getElementById("name").value;
        var locfrom = document.getElementById("locfrom").value;
        var locto = document.getElementById("locto").value;
        var datefrom = document.getElementById("datefrom").value;
        var dateto = document.getElementById("dateto").value;
        var instructions = document.getElementById("instructions").value;
        var travelmode = $('#travelmode option:selected').text();

        var contactdiv = '<tr class="data-contact-person">' +
                '<td><input type="checkbox"class="form-control f-check01" /></td>' +
                '<td><input type="text" value="' + name + '" class="form-control f-name01" /></td>' +
                '<td><input type="text" value="' + locfrom + '" class="form-control l-from01" /></td>' +
                '<td><input type="text" value="' + locto + '" class="form-control l-to01" /></td>' +
                '<td><input type="text" value="' + datefrom + '" class="form-control d-from01" /></td>' +
                '<td><input type="text" value="' + dateto + '" class="form-control d-to01" /></td>' +
                '<td>@Html.DropDownList("TravelMode", null, htmlAttributes: new { @id = "travelmode", @class = "form-control t-mode01" })</td>' +
                '<td><input type="text" value="' + instructions + '" class="form-control l-ins01" /></td>' +

                '</tr>';
        $('#maintable').append(contactdiv);

        ////Clear fields
        $('#name').val('').focus();
        $('#locfrom,#locto,#datefrom,#dateto,#travelmode,#instructions').val('');
    });

I just want a code Which I can write in Javascript table tags for dropdowns having values from DataBase.


What I have tried:

//For Travel Details
$(document).on("click", ".classAdd", function () {

    var rowCount = $('.data-contact-person').length + 1;

    var name = document.getElementById("name").value;
    var locfrom = document.getElementById("locfrom").value;
    var locto = document.getElementById("locto").value;
    var datefrom = document.getElementById("datefrom").value;
    var dateto = document.getElementById("dateto").value;
    var instructions = document.getElementById("instructions").value;
    var travelmode = $('#travelmode option:selected').text();

    var contactdiv = '<tr class="data-contact-person">' +
            '<td><input type="checkbox"class="form-control f-check01" /></td>' +
            '<td><input type="text" value="' + name + '" class="form-control f-name01" /></td>' +
            '<td><input type="text" value="' + locfrom + '" class="form-control l-from01" /></td>' +
            '<td><input type="text" value="' + locto + '" class="form-control l-to01" /></td>' +
            '<td><input type="text" value="' + datefrom + '" class="form-control d-from01" /></td>' +
            '<td><input type="text" value="' + dateto + '" class="form-control d-to01" /></td>' +
            '<td>@Html.DropDownList("TravelMode", null, htmlAttributes: new { @id = "travelmode", @class = "form-control t-mode01" })</td>' +
            '<td><input type="text" value="' + instructions + '" class="form-control l-ins01" /></td>' +

            '</tr>';
    $('#maintable').append(contactdiv);

    ////Clear fields
    $('#name').val('').focus();
    $('#locfrom,#locto,#datefrom,#dateto,#travelmode,#instructions').val('');
});
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100