HOW TO A DYNAMIC ROW IN A TABLE USING RAZOR VIEW IN MVC4?

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 8721  View(s)
Ratings:
Rate this:

I have a table in the razor view and this table contain some Html controls like below:-

  <table style="text-align: center; width: 680px; background-color: White;" rules="all" id="tableId">
                <tr>
                    <td style="width: 20px;"></td>
                    <td>
                        <label>
                            Campaign Name</label>
                    </td>
                    <td>
                        <label>
                            Valid Date From</label>
                    </td>
                    <td>
                        <label>
                            Valid Date To</label>
                    </td>
                    <td>
                        <label>
                            Is Active</label>
                    </td>
                    <td>
                        <label>
                            ContractPDF</label>
                    </td>
                    <td>
                        <label>
                            Browse Contract</label>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.HiddenFor(m => m.Customer_ID)
                    </td>
                    <td class="trBorder">
                        @Html.TextBoxFor(m => m.Customer_Name)
                    </td>
                    <td class="trBorder">
                        @Html.TextBoxFor(m => m.ValidFrom, new { @class = "datepicker" })
                    </td>
                    <td>
                        @Html.TextBoxFor(m => m.ValidTo, new { @class = "datepicker" })
                    </td>
                    <td>
                        @Html.CheckBox("gridChkCampaignIsActive", m => m.IsActive)
                    </td>
                    <td>
                        @Html.TextBoxFor(m => m.ContractDocumentPath)
                    </td>
                    <td>
                        @Html.ActionLink("Browse", "FileUpload")
                    </td>
                </tr>
            </table>

Add Customer button

<input type="button" value="Add Customer" name="btnAddCustomer" id="btnAddCustomer" style="margin-left: 20px;"/>

My problem is that I want this table like a grid control and when I click on "Add Customer" button, a blank row will be added in the grid control.

please Help me!

Any Help Will be Appreciated

  1. Dag Hammarskjold

    Post:24

    Points:168
    Re: How to a dynamic row in a table using Razor view in MVC4?

    First of all , you must add jquery library to your code :

    $("#btnAddCustomer").click(function(){
        $("#tableId tr:last").after("<tr>...</tr><tr>...</tr>");
        });

    Or you can use this:

    $("#btnAddCustomer").click(function(){
    $('#tableId').append('<tr><td>my data</td><td>more data</td></tr>');
    });

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!