Cześć ucze sie ASP.NET i mam problem dlaczego nie działa wyszukiwanie. Dane sa przekazywane do zmienej search a mimo wszystko pobiera mi i tak 4 rzeczy
public IActionResult Index(string search)
{
KingRepository repo = new KingRepository(_ctx);
CategoryRepository cato = new CategoryRepository(_ctx);
var kings = repo.GetAllKing();
if (!string.IsNullOrEmpty(search))
{
kings = kings.Where(m => m.Name.ToLower().Contains(search.ToLower())).ToList();
}
var categories = cato.GetCategies();
PanelViewModel vm = new PanelViewModel()
{
Kings = kings,
Categories = categories
};
return View(vm);
}
@model web.ViewModels.PanelViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<form method="get" asp-action="Index" asp-controller="Home">
<input id="searchTxt" type="search" value="" />
<button type="button" id="searchBtn">Search</button>
</form>
<div id="TableCon">
<table>
<thead>
<tr>
<th></th>
<th>Nazwa</th>
<th>Epoka</th>
<th>Akcja</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Kings)
{
<tr>
<td></td>
<td>@item.Name</td>
<td>@item.Category.Name</td>
<td><button>Edytuj</button> <button>Usuń</button></td>
</tr>
}
</tbody>
</table>
</div>
<script>
$("#searchBtn").click(function () {
var searchValue = $("#searchTxt").val();
$.ajax({
url: "/Home/Index",
data: {
search: searchValue
}
}).done(function (response) {
$("#TableCon").html(response);
alert("Treu")
}).fail(function (XMLHttpRequest, textStatus, errorThrown) {
alert("FAIL")
});
});
</script>
</body>