Have an API that displays a range of records based on the StartDate and EndDate. The GetRange action looks like this:
[HttpGet]
public IActionResult GetRange([FromQuery] DateRange parameters)
{
var range = _context.Products.AsQueryable();
if (parameters.StartDate.HasValue)
{
range = range.Where(o => o.StartDateTime >= parameters.StartDate.Value);
}
if (parameters.EndDate.HasValue)
{
range = range.Where(o => o.StartDateTime <= parameters.EndDate.Value);
}
var result = range.ToList();
return Ok(result);
}
This works (when tested with swagger UI), now in the ASP.NET web client, I need to filter the records by a specified date ranged. The action (controller) on the client looks like this
[HttpGet]
public IActionResult GetRange(DateTime startdate, DateTime enddate)
{
string baseUrl = _config.GetValue<string>("ApiSettings:BaseUrl");
//create a list for the range of products
List<CourseViewModel> productList = new List<CourseViewModel>();
HttpResponseMessage response = _client.GetAsync($"{baseUrl}/Products/GetRange?StartDate={startdate}&&EndDate={enddate}").Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
//deserialized the "data"
productList = JsonConvert.DeserializeObject<List<CourseViewModel>>(data);
}
return View(productList);
}
Then added the two input fields and submit button to the razor page (index.cshtml)
<form method="post">
Startdata : <input type="datetime" name="startdate" />
Enddata : <input type="datetime" name="enddate" />
<input type="submit" value="Submit" class="btn btn-success" />
<table class="table">
<!—Code to display headers and the data not shown -->
</table>
</form>
The client populates the page with all records but when I enter starDate and EndDate then click submit
Get a Error 405 and thus the client-side GetRange() action never gets executed.
Do I even need to encapsulate the table with a ?
What tells the submit what Action to execute?
Any idea what I am doing wrong?