Click here to Skip to main content
15,074,394 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 mvc with sql server<br/>
i try using document.getElementById but still useless

        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_LayoutGentelella.cshtml";
        <table class="table table-striped" id="DSWH_ItemGroubs">
                    <th>Group Name</th>
                    <th>Create By</th>
                    <th>Create Date</th>
                    <th>Last Modified Date</th>
                    <th>Last Modified By</th>
                @foreach (gentelella.Models.DSWH_ItemGroubs item in Model)
                        @*<th scope="row">@item.ItemGroup_ID</th>*@
                        <td><input type="button" value="Remove" onclick="Remove(this)" /></td>
                    <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>
        <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=""></script>
        <script type="text/javascript" src=""></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 = $(row.insertCell(-1));
                cell = $(row.insertCell(-1));
                cell = $(row.insertCell(-1));
                cell = $(row.insertCell(-1));
                //Add Button cell.
                cell = $(row.insertCell(-1));
                var btnRemove = $("<input />");
                btnRemove.attr("type", "button");
                btnRemove.attr("onclick", "Remove(this);");
                //Clear the TextBoxes.
            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.
            $("body").on("click", "#btnSave", function () {
                //Loop through the Table rows and build a JSON array.
                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();
                    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.");

What I have tried:

i try using document.getElementById but still useless
Updated 15-Mar-18 0:00am
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

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