Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Team

I am trying to do create a search filter, so each time i am trying to find that value by using an int, it create a string null format as stating incorrect string format. How do i resolve this issue, my logic is below?

What I have tried:

C#
<pre> //GET: SearchPeople-ID.

        public ActionResult SearchPeopleDetails()
        {
            
            RegCoursesViewModel regCoursesView = new RegCoursesViewModel();
            
            return View(cb.RegPeopleLists.ToList());
        }

        // GET://Passing-Data-Back as Json.
        public JsonResult GetSearchingData(string SearchBy, string SearchValue)
        {
            List<eNtsaRegPeopleLists> regPeopleLists = new List<eNtsaRegPeopleLists>();
            if(SearchBy == "ID")
            {
                try
                {
                    int Id = Convert.ToInt32(SearchValue);
                    regPeopleLists = cb.RegPeopleLists.Where(v => v.LoginID == Id || SearchValue == null).ToList();
                }catch(FormatException)
                {
                    Console.WriteLine("{0} Is Not A ID ", SearchValue);
                }return Json(regPeopleLists, JsonRequestBehavior.AllowGet);
            }
            else
            {
                regPeopleLists = cb.RegPeopleLists.Where(v => v.Name.StartsWith(SearchValue) || SearchValue == null).ToList();
                return Json(regPeopleLists, JsonRequestBehavior.AllowGet);
            }
            
        }










@model IEnumerable<eNtsaRegistrationTraining.Models.eNtsaRegPeopleLists>

<br />
<br />
<div class="form-group row float-right">
    <form class="form-group ml-lg-auto">
        <div class="input-group input-group-sm">
            <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search" id="SearchLv">
            <div class="input-group-append">
                <button class="btn btn-navbar" type="submit">
                    class="fas fa-search">
                </button>
            </div>
        </div>
    </form>
</div>

<!--Select-ID-->
<select id="SearchBy">
    <option value="ID">LoginID</option>
    <option value="Name">Name</option>
    <option value="Roles">Roles</option>

</select>
<br />
<br />
<table class="table table-bordered">
    <thead>
        <tr>
            <th>LoginID</th>
            <th>Name</th>
            <th>Roles</th>
            <th>SISID</th>
            <th>LastActivity</th>
            <th>TotalActivity</th>
        </tr>
    </thead>
    <!--Tbody here-->
    <tbody id="DataSearching">
        @foreach(var Item in Model)
        {
           <tr>
              <td>@Item.LoginID</td>
               <td>@Item.Name</td>
               <td>@Item.Role</td>
               <td>@Item.SISID</td>
               <td>@Item.LastActivity</td>
               <td>@Item.TotalActivity</td>
           </tr>
        }
    </tbody>
</table>

<!--Javascript functionality for filter search-->
<script src="~/Scripts/jquery-3.4.1.min.js"></script> 
<script type="text/javascript">
     $(document).ready(function () {
        $("#SearchLv").keyup(function () {
            var SearchBy = $("#SearchBy").val();
            var SearchValue = $("#Search").val();
            var SetData = $("#DataSearching");
            SetData.html("");
            debugger;
            $.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>" +
                                "<td>" + value.LoginID + "</td>" +
                                "<td>" + value.Name + "</td>" +
                                "<td>" + value.Role + "</td>" +
                                "<td>" + value.SIS_ID + "</td>" +
                                "<td>" + value.LastActivity + "</td">  +
                                "<td>" + value.TotalActivity + "</td>"
                                "</tr>";
                            SetData.append(Data);

                        });
                    }
                }
            });
        });
    });
</script>  
Posted
Updated 22-Sep-20 0:29am
v2
Comments
F-ES Sitecore 22-Sep-20 5:00am    
Put a breakpoint inside the GetSearchingData method, what is the value of SearchBy and SearchValue?
gcogco10 22-Sep-20 5:07am    
SearchBy=ID, SearchValue="undefined"
F-ES Sitecore 22-Sep-20 6:06am    
$("#Search") won't be finding an element that has a "value" property. You haven't posted the relevant bit of the view that outputs the "Search" element so it's hard to say why that is, but that's what you need to look at next.
gcogco10 22-Sep-20 6:30am    
@F-ES SiteCore i have included full Html for more clarity and understanding, maybe i omitted something along the way.
F-ES Sitecore 22-Sep-20 8:28am    
There is no "Search" element there, did you mean SearchLv?

var SearchValue = $("#SearchLv").val();

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