Click here to Skip to main content
15,900,386 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Working with MVC, Razor pages. Trying to create a grid with a functionality to insert rows within the grid (not against a pop up or a separate page) with a click of + and delete a row on - . The counter is not working on the front end (Razor HTML). Code given below. Any one can help please?


What I have tried:

@model New_Booking_Home.Models.Booking

@{
    Layout = null;
}

<!DOCTYPE html>
<body>
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="container">
          <div class="row">
                <p></p>
                <table id="myTable" style="width:100%">
                    <tr>
                        <td>
                            <button value="ADD" type="button" class="btnplus tooltip" onclick="myCreateFunction(this)">class="fa fa-plus"></button>
                        </td>
                        <td>
                            <div>Container Type</div>
                        </td>
                        <td>
                            <div>Quantity</div>
                        </td>
                    </tr>


                    @if (Model != null)
                    {
                        int i = 0;
                        foreach (var item in Model.cargolist2.ToList())
                        {
                            <!-- for (int i = 0; i < Model.cargolist2.Count; i++)-->
                            <tr>
                                <td>


                                    <button value="delete" type="button" class="btndel tooltip" onclick="deleteRow(this)">^__i class="fa fa-trash"></button>


                                </td>
                                <td>
                                    @Html.EditorFor(model => model.cargolist2[i].CntrtypeID, new { htmlAttributes = new { @class = "form-control textbx" } })
                                    @Html.ValidationMessageFor(model => model.cargolist2[i].CntrtypeID, "", new { @class = "text-danger" })
                                </td>
                                <td>
                                    @Html.EditorFor(model => model.cargolist2[i].Qty, new { htmlAttributes = new { placeholder = "Enter quantity", @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.cargolist2[i].Qty, "", new { @class = "text-danger" })
                                </td>
                            </tr>
                        }
                    }
                </table>
            </div>
            <div>
                <input type="submit" class="genbtn" value="Submit" />
        </div>
    }


    <script>
    function myCreateFunction() {
        var table = document.getElementById("myTable");
        var x = document.getElementById("myTable").rows.length;

        x = x + 1;

        var row = table.insertRow();
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);

        cell1.innerHTML =  '<button class="btndel tooltip" type="button" value="Delete" onclick="deleteRow(this)"></button>';
        cell2.innerHTML = '@Html.EditorFor(model => model.cargolist2[' + x + '].CntrtypeID, new { htmlAttributes = new { @class = "form-control textbx" } })                                @Html.ValidationMessageFor(model => model.cargolist2[' + x + '].CntrtypeID, "", new { @class = "text-danger" })';
        cell3.innerHTML = '@Html.EditorFor(model => model.cargolist2[' + x + '].Qty, new { htmlAttributes = new { placeholder = "Enter quantity", @class = "form-control" } })                         @Html.ValidationMessageFor(model => model.cargolist2[' + x + '].Qty, "", new { @class = "text-danger" });';
}



  function deleteRow(r) {
    var i = r.parentNode.parentNode.rowIndex;
    document.getElementById("myTable").deleteRow(i);
  }


    </script>
</body>
</html>
Posted
Updated 21-Aug-19 2:15am

You have to appreciate the separation of server code and client code. Your server code (everything after @) runs first on the server, the resulting html is then sent to the client to execute, and at that point your js runs, so you can't have server code run dynamically in your javascript. View the source of your page and look at the javascript for the line that starts

cell2.innerHTML = ...


That is the exact code that runs every time you call myCreateFunction.

What you have to do is create the input elements using the same html format that EditorFor does. Again if you view the source you'll see what that format is, the important bit is the "name", which will be the name derived from your modal with a number in square brackets that indicates it is part of an array, so you'll have elemenets with names like "myElement[0]", "myElement[1]" etc. When elements like that are submitted with your form they are parsed into an array by .net.

Anyway, there is a basic example that shows how to do this here;

ASP.NET MVC3 Dynamically added form fields model binding[^]

It shows how to create the input html using the right format, just adapt that for your own variable names.
 
Share this answer
 
v2
Comments
Maciej Los 21-Aug-19 8:16am    
5ed!
Dave Kreskowiak 21-Aug-19 10:24am    
"Your server code (everything after @) runs first on the client..."??

Don't you mean "runs on the server"?
F-ES Sitecore 21-Aug-19 10:37am    
Yes, that's what I said

*coughs nervously*
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900