本文介绍了在数据库ASP.NET MVC中保存空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>





我尝试了什么:



i尝试使用document.getElementById但仍无用



What I have tried:

i try using document.getElementById but still useless

推荐答案




这篇关于在数据库ASP.NET MVC中保存空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:08