I'm doing a project in which at the end of an activity you get a PopUp that when you click on it, it is sent via ajax - api to the database:
Activity Model:
namespace PSCORE.DataBase.Models
{
public class Activitat
{
[Key]
[MaxLength(100)]
public string Nom {get;set;}
//public virtual Regne Regne {get;set;}
public int Vegades_fet {get;set;} = 0;
public int Vegades_fet_be {get;set;} = 0;
public int Vegades_fet_malament {get;set;} = 0;
public int Progres {get;set;} = 0;
public float Temps {get;set;} = 0;
}
}
Api Activity - GurdarInformation:
namespace PSCORE.Controllers
{
[Produces("application/json")]
[Route("api/[controller]")]
public class GuardarInformacion : Controller
{
[HttpGet]
public ActionResult Index(){
return Ok();
}
private readonly ApplicationDbContext dbContext;
public GuardarInformacion()
{
dbContext = new ApplicationDbContext();
}
[HttpPost]
public void Guardar([FromBody] ActivitatObject activitat_r)
{
Activitat activitat = new Activitat
{
Nom = activitat_r.Nombre
};
dbContext.Activitats.Add(activitat);
dbContext.SaveChanges();
}
public class ActivitatObject {
public string Nombre {get;set;}
}
}
}
Jquery's ajax code:
$(document).ready(function() {
$(".botonacabar").on("click",function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "http://localhost:5000/api/GuardarInformacion",
data:
{
Nombre: "Prueba"
},
dataType: 'json'
});
});
});
I have put a Debug point in the Post of the Api to see if the information arrived or what information arrived, but I got the following error: