Click here to Skip to main content
15,029,188 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i try to save the values that in text boxes in data base but it save null, not the value this code in view save null in SQL ,
it give me saving object html input element instead of value, an asp.net mvc with sql server<br/>
i try using document.getElementById but still useless
 
<br/>

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_LayoutGentelella.cshtml";
    }
        <h2>Index</h2>
        
        <table class="table table-striped" id="DSWH_ItemGroubs">
            <thead>
                <tr>
        
                    @*<th>#</th>*@
                    <th>Group Name</th>
                    <th>Create By</th>
                    <th>Create Date</th>
                    <th>Last Modified Date</th>
                    <th>Last Modified By</th>
                    <th>Edit</th>
        
                </tr>
            </thead>
            <tbody>
                @foreach (gentelella.Models.DSWH_ItemGroubs item in Model)
                    {
                    <tr>
                        @*<th scope="row">@item.ItemGroup_ID</th>*@
                        <td>@item.ItemGroup_Name</td>
                        <td>@item.Create_By</td>
                        <td>@item.Create_Date</td>
                        <td>@item.Last_ModifiedDate</td>
                        <td>@item.Last_ModifiedBy</td>
                        <td><input type="button" value="Remove" onclick="Remove(this)" /></td>
                    </tr>
                }
            </tbody>
            <tfoot>
                <tr>
                    <td><input type="text" id="txtItemGroupName" /></td>
                    <td><input type="text" id="txtCreateBy" /></td>
                    <td><input type="text" id="txtCreateDate" /></td>
                    <td><input type="text" id="txtLastModifiedDate" /></td>
                    <td><input type="text" id="txtLastModifiedBy" /></td>
                    <td><input type="button" id="btnAdd" value="Add" /></td>
                </tr>
            </tfoot>
        </table>
        <br>
        <input type="button" id="btnSave" value="Save All" />
        @*<script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/json2.js"></script>*@
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
        <script type="text/javascript">
        
            $("body").on("click", "#btnAdd", function () {
                var txtItemGroupName = $('txtItemGroupName');
                var txtCreateBy = $("#txtCreateBy");
                var txtCreateDate = $("#txtCreateDate");
                var txtLastModifiedDate = $("#txtLastModifiedDate");
                var txtLastModifiedBy = $("#txtLastModifiedBy");
        
                //Get the reference of the Table's TBODY element.
                var tBody = $("#DSWH_ItemGroubs > TBODY")[0];
        
                //Add Row.
                var row = tBody.insertRow(-1);
        
                //Add cell.
                var cell = $(row.insertCell(-1));
                cell.html(txtItemGroupName.val());
        
                cell = $(row.insertCell(-1));
                cell.html(txtCreateBy.val());
        
                cell = $(row.insertCell(-1));
                cell.html(txtCreateDate.val());
        
                cell = $(row.insertCell(-1));
                cell.html(txtLastModifiedDate.val());
        
                cell = $(row.insertCell(-1));
                cell.html(txtLastModifiedBy.val());
        
                //Add Button cell.
                cell = $(row.insertCell(-1));
                var btnRemove = $("<input />");
                btnRemove.attr("type", "button");
                btnRemove.attr("onclick", "Remove(this);");
                btnRemove.val("Remove");
                cell.append(btnRemove);
        
                //Clear the TextBoxes.
                txtItemGroupNamee.val("");
                txtCreateBy.val("");
                txtCreateDate.val("");
                txtLastModifiedDate.val("");
                txtLastModifiedBy.val("");
            });
        
            function Remove(button) {
                //Determine the reference of the Row using the Button.
                var row = $(button).closest("TR");
                var name = $("TD", row).eq(0).html();
                if (confirm("Do you want to delete: " + name)) {
                    //Get the reference of the Table.
                    var table = $("#DSWH_ItemGroubs")[0];
        
                    //Delete the Table row using it's Index.
                    table.deleteRow(row[0].rowIndex);
                }
            };
        
            $("body").on("click", "#btnSave", function () {
                //Loop through the Table rows and build a JSON array.
                alert(txtItemGroupName)
                var items = new Array();
                $("#DSWH_ItemGroubs TBODY TR").each(function () {
                    var row = $(this);
                    var item = {};
                    item.ItemGroupName = row.find("TD").eq(0).html();
                    item.CreateBy = row.find("TD").eq(1).html();
                    item.CreateDate = row.find("TD").eq(2).html();
                    item.LastModifiedDate = row.find("TD").eq(3).html();
                    item.LastModifiedBy = row.find("TD").eq(4).html();
                    items.push(item);
                });
        
                $.ajax({
                    type: "POST",
                    url: "/Groups/AddGroup",
                    data: JSON.stringify(items),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                        alert(r + " record(s) inserted.");
                    }
                });
            });
        </script>


What I have tried:

i try using document.getElementById but still useless
Posted
Updated 15-Mar-18 0:00am
Comments
F-ES Sitecore 9-Mar-18 5:08am
   
var txtItemGroupName = $('txtItemGroupName');

should be

var txtItemGroupName = $('#txtItemGroupName');

not sure that'll fix your issue though, you can't just dump lots of code and say it "doesn't work". Do some debugging and find out what variable etc isn't being populated when you expect it to be populated, and at what point in the code.

1 solution

Hi,
When you want to get a value of any textbox, you should use .val() like :
var txtItemGroupName = $('txtItemGroupName').val();


Hope this helps!
   

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