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>
public ActionResult SearchPeopleDetails()
{
RegCoursesViewModel regCoursesView = new RegCoursesViewModel();
return View(cb.RegPeopleLists.ToList());
}
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>