Hi trisetia302,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Model
public class EmployeeModel
{
public string Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
List<EmployeeModel> obats = new List<EmployeeModel>();
return View(obats);
}
[HttpPost]
public IActionResult CekStockObat(int parameter1, int parameter2)
{
List<EmployeeModel> obats = new List<EmployeeModel>();
using (SqlConnection con = new SqlConnection("Server=.;DataBase=Northwind;UID=sa;PWD=pass@123"))
{
using (SqlCommand cmd = new SqlCommand(null))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "Select * From Employees Where EmployeeID >=" + parameter1 + " And EmployeeID <=" + parameter2;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
obats.Add(new EmployeeModel
{
Id = rdr["EmployeeID"].ToString(),
Name = rdr["FirstName"].ToString(),
City = rdr["City"].ToString(),
Country = rdr["Country"].ToString()
});
}
}
}
}
return View("Index", obats.ToList());
}
}
View
@model IEnumerable<Filter_Between_Integer_MVC_Core.Models.EmployeeModel>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form asp-action="CekStockObat" asp-controller="Home" method="post">
<table>
<tr>
<td>
Enter Parameter
<input type="text" id="parameter1" name="parameter1" class="form-control" placeholder=">=0" />
<input type="text" id="parameter2" name="parameter2" class="form-control" placeholder="<=10" />
<button type="submit" class="btn btn-success btn-sm"> Cek Stock</button>
</td>
</tr>
</table>
</form>
<hr />
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Id)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.City)</td>
<td>@Html.DisplayFor(modelItem => item.Country)</td>
</tr>
}
</tbody>
</table>
</body>
</html>
Screenshot