jQuery和.Net CRUD搜索

时间:2019-06-16 21:36:15

标签: jquery asp.net .net

我的jQuery函数有问题,无法从列表中搜索与输入字符串匹配的元素。始终表明没有找到数据。

我尝试了各种修改,但没有帮助。

这是jQuery函数的代码

      $(document).ready(function () {
    $("#SearchBtn").click(function () {
        console.log("ttt");
        var SearchBy = $("#SearchBy").val();
        var SearchValue = $("#Search").val();
        var SetData = $("#SetStudentList");
        SetData.html("");
        $.ajax({
            type: "post",
            url: "/Home/GetSearchingData?SearchBy=" + SearchBy + "&SearchValue=" + SearchValue,
            contentType: "html",
            success: function (result) {
                if (result.length == 0) {
                    SetData.append('<tr style="color:red"><td colspan="3">No Match Data</td></tr>')
                }
                else {
                    $.each(result, function (index, value) {
                        var Data = "<tr class='row_" + StudentList[i].StudentId + "'>" +
                            "<td>" + StudentList[i].StudentId + "</td>" +
                            "<td>" + StudentList[i].StudentName + "</td>" +
                            "<td>" + StudentList[i].Email + "</td>" +
                            "<td>" + StudentList[i].DepartmentName + "</td>" +
                            "<td>" + "<a href='#' class='btn btn-warning' onclick='EditStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-edit'></span></a>" + "</td>" +
                            "<td>" + "<a href='#' class='btn btn-danger' onclick='DeleteStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-trash'></span></a>" + "</td>" +
                            "</tr>";
                        SetData.append(Data);

                    });
                }
            }

        });
    });

});

搜索按钮的HTML:

          <b>Search By : </b>
    <select id="SearchBy">
        <option value="StudentID">Student ID</option>
        <option value="StudentName">Student Name</option>
        <option value="Email">Email</option>
        <option value="Department">Department</option>
    </select> <br /><br />
    @Html.TextBox("Search")<input type="submit" id="SearchBtn" value="Search" /> <br /><br />

来自控制器的方法(搜索以及getlist和getbyID,因为我什至在单击搜索按钮后甚至试图调用getlist,但始终有信息“无匹配结果”):

    public JsonResult GetSearchingData (string SearchBy, string SearchValue)
    {
        List<StudentViewModel> StuList = new List<StudentViewModel>();
        if (SearchBy == "Student ID")
        {
            try
            {
                int Id = Convert.ToInt32(SearchValue);
                StuList = db.tblStudent.Where(x => x.StudentId == Id || SearchValue == null).Select(x => new StudentViewModel
                {
                    StudentId = x.StudentId,
                    StudentName = x.StudentName,
                    Email = x.Email,
                    DepartmentName = x.tblDepartment.DepartmentName
                }).ToList();

            }
            catch (FormatException)
            {
                Console.WriteLine("{0} Is Not A ID ", SearchValue);
            }
            return Json(StuList, JsonRequestBehavior.AllowGet);
        }
        else
        {
            StuList = db.tblStudent.Where(x=>x.StudentName.Contains(SearchValue) || SearchValue == null).Select(x => new StudentViewModel
            {
                StudentId = x.StudentId,
                StudentName = x.StudentName,
                Email = x.Email,
                DepartmentName = x.tblDepartment.DepartmentName
            }).ToList();
            return Json(StuList, JsonRequestBehavior.AllowGet);
        }
    }

         public JsonResult GetStudentList()
    {
        List<StudentViewModel> StuList = db.tblStudent.Where(x => x.IsDeleted == false).Select(x => new StudentViewModel
        {
            StudentId = x.StudentId,
            StudentName = x.StudentName,
            Email = x.Email,
            DepartmentName = x.tblDepartment.DepartmentName
        }).ToList();

        return Json(StuList, JsonRequestBehavior.AllowGet);
    }

    public JsonResult GetStudentById (int StudentId)
    {
        tblStudent model = db.tblStudent.Where(x => x.StudentId == StudentId).SingleOrDefault();
        string value = string.Empty;
        value = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        });
        return Json(value, JsonRequestBehavior.AllowGet);
    }

任何帮助将不胜感激。

0 个答案:

没有答案