Click here to Skip to main content
14,695,617 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:

<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">
                    
                </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 1: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();
gcogco10 22-Sep-20 8:39am
   
@F-ES Sitecore i did change it to var SearchValue = $("#SearchLv").val(); but i still get the same issue, the issue each time i insert using int, no search filter is finding the ID from the record, but when i used string value, it throws an exception inside GetSearchData method on the controller. "incorrect string format value".
F-ES Sitecore 22-Sep-20 8:42am
   
If it has to be an int value as it does Convert.ToInt32. If you want to gracefully handle when people use non-ints then use int.TryParse instead. That will return "false" if the value can't be converted to an int, making it easier to show an error, or default to another value, whichever you want to do.

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