无法从html中检索数据

时间:2017-07-31 07:03:21

标签: javascript c# jquery ajax asp.net-mvc

至于我的下面的代码,我无法从tr获取sectionID,我需要在每个删除按钮上获得sectionID的动态ID,但它总是给我null

这是Jquery脚本:

         <script>
      $(function () {
       $('.btnDelete').click(function () {
      var sectionID = $(this).closest('tr').find('.sectionID');
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: 'CheckSectionIDagainststudentID',
        data: JSON.stringify({ sectionID: sectionID.html() }),
        success: function (data) {
            if (data == true) {
                $.alert("Cannot delete | Students exsisting in this 
         Section!");
            }
            else if (data == false) {
                var secCode = JSON.parse(this.data);
                var code = sectionid['sectionid'];
                window.location.href = "SectionDelete?Page=data&sectionid=" 
    + code;
            }
        },
        failure: function (response) {
            $('#result').html(response);
        }
    });
});
        });
      </script>

这是Razor Page

 @foreach (var item in Model)
    {
        <tr class="sectionID">
            <td >
                @Html.DisplayFor(modelItem => item.sectionID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.name)

            </td>
            <td class="secCode">

                <button style="width:49.5%" ID="Button2" type="button" onclick="location.href='@Url.Action("SectionEdit", "Section",new { id = item.sectionID, name = item.name })'">Edit</button>
                <button style="width:49.5%" ID="deletebtn" runat="server" type="button" onclick="location.href='@Url.Action("SectionDelete", "Section",new { id = item.sectionID, name = item.name })'">Delete</button>
                <button class="btnDelete">Delete</button>
            </td>

        </tr>
    }

这是我需要将数据传递给

的控制器
[HttpPost]
        public ActionResult CheckSectionIDagainststudentID(string sectionID)
        {

            return Json(sectionID);


        }

2 个答案:

答案 0 :(得分:0)

使用字符串数组:

ORA-14551: cannot perform a DML operation inside a query

答案 1 :(得分:0)

根据您的问题,您无法从var sectionID = $(this).closest('tr').find('.sectionID');获取价值
因此,这是一种可以实现结果的方法

//Your Dynamic Button should look like this
//in value Bind your sectionID @ MVC
 <button class="btnDelete" value="5" onclick="AjaxDelete(this)">Delete</button>

 //For Function
      function AjaxDelete(values) {
    //Write this in Ajax
    //Fetch Value from attribute value
     var sectionID = $(values).attr("value"); // you will get 5 as value
     //Your Ajax Call with SectionId as Parameter
    }

编辑: 因为你有价值 你可以通过下面的代码分割价值 其中string是你的sectionID

的var
 if ((~string.indexOf('\n '))) {
                    string= string.split('\n ')[1].split('\n')[0];
                }