I have a solution with 2 project
1. Web Service
2. ASP.NET (MVC)
In the view I have an input type="date" where I choose a date and through communication with the WS it has to search in a DB for the records that in their date column their data matches that date that I entered in the view.
HomeController
[HttpPost]
public async Task<ActionResult> Index(string date)//Get the date from the input type date
{
SearchDate lst = new SearchDate();
SearchDateResponse lstR = new SearchDateResponse();
lst.StartDate = date;
var r = await _servicio_api.Lista(lst.StartDate);
lstR.factura = r;
lst.lstR = lstR;
return View(lst);
}
SERVICIO_API e interfaz IServicio_API
//Services ASP.NET Folder
public async Task<List<Factura>> Lista(string date)//Receive the date from the "HomeController" controller var r = await _api_service.List(lst.StartDate);
{
List<Factura> facturaList = new List<Factura>();
GetFacturaRequest req = new GetFacturaRequest();
req.fecha = date;
var content = new StringContent(JsonConvert.SerializeObject(req), Encoding.UTF8, "application/json");
var client = new HttpClient();
client.BaseAddress = new Uri(_baseUrl);
var response = await client.PostAsync("/api/Factura", content);
if (response.IsSuccessStatusCode)
{
var json_respuesta = await response.Content.ReadAsStringAsync();
List<Factura> resultado = JsonConvert.DeserializeObject<List<Factura>>(json_respuesta);
facturaList.AddRange(resultado);
}
return facturaList;
}
public interface IServicio_API
{
Task<List<Factura>> Lista(string date);
}
FacturaController en el WS
[HttpPost]
public IActionResult Index(GetFacturaRequest date)//Receives the date (date) from Service_API
{
return Ok(facturaService.Get(date));//Sends the date to the Get method of FacturaService
}
FactureServices.cs (Services ASP.NET Folder)
public IEnumerable<Factura> Get(GetFacturaRequest datos)//Receives the date from "api/Factura" in FacturaController.cs
{
return context.Facturas.Where(x => x.fechaEmision == DateTime.Parse(datos.fecha));
//Find how many records in the database exist with the date entered in the input
}
}
public interface IFactura
{
IEnumerable<Factura> Get(GetFacturaRequest datos);
}
Then from here it returns to the if of the second piece of code that I uploaded, that is, this:
if (response.IsSuccessStatusCode)
{
var json_respuesta = await response.Content.ReadAsStringAsync();
List<Factura> resultado = JsonConvert.DeserializeObject<List<Factura>>(json_respuesta);
facturaList.AddRange(resultado);
}
return facturaList;
What it does here is that it evaluates if the request to the service was true, it returns the records if it found it or not. (If there are connection problems with the WS, the if is skipped and the execution ends)
And to finish, it returns to the beginning and returns the date that I entered and the records that it found with that date.
Then when I try to print in this case the date that the ViewModel brings to the view it seems as if it brought it but it is not shown, that is, in the following image it is seen that the date is in the @Model... in the label but when the execution finishes the data is not shown in the view.
In the end I try to do 2 things:
- In the last image the date (StarDate) I want to print it in the view.
- If it is possible
Count = 2print it also since it has been the number of records that were found in the DB with that date.
ViewModel in the view
@model PORTAL.ViewModels.SearchDate //ViewModel
Image of the view (HTML) where the data should appear.
The red line is where the date should appear, to the right you can see the empty <b></b> and to the right of it another image of <b></b> but in the index.cshtml
The date should appear as the numbers 310/400 that are on the other label (static data)