Witam, mam problem. Chciałem stworzyć klienta z wykorzystaniem ajax, ktory pobierze liste studentów oraz doda nowego studenta. Działa mi aktualnie tylko pobieranie listy, natomiast dodanie nowego juz nie. Siedze juz kilka godzin i niewiem czy błąd jest w kodzie czy w może ustawieniach. Proszę o wskazówki i z góry dziękuję za odpowiedź.
Widok:
@{
ViewBag.Title = "Students API";
}
<br />
@using (Html.BeginForm())
{
<br /><button id="GetStudents">Get Students</button> <br />
<br /><button id="btnAddStudent">Add Student</button> <br />
<div id="Emessage"></div>
}
@section Scripts {
<script type="text/javascript">
$(function () {
$('#GetStudents').on("click", function () {
$.ajax({
type: "GET",
url: '/api/API',
datatype: 'json',
cache: false,
})
.success(function (data) {
var info = "";
for (var i = 0; i < data.length; i++) {
info += "<hr />";
info += "<b>Student ID : </b>" + data[i].Id + "<br />";
info += "<b>Name : </b>" + data[i].Name + "<br />";
info += "Surname : </b>" + data[i].Surname;
info += "<b>Address : </b>" + data[i].Address;
info += "<b>Field of Study : </b>" + data[i].Field_of_Study;
info += "<b>Year of Study : </b>" + data[i].Year_of_Study;
info += "<b>Date_of_birth : </b>" + data[i].Date_of_birth;
}
$('#Emessage').html(info);
})
.error(function (xhr, ajaxOptions, thrownError) {
$('#Emessage').html("Error : An error occured");
});
return false;
});
$('#AddStudent').on("click", function () {
var Students = {Name: 'Arnold', Surname: 'Schwarzenegger' };
$.ajax({
type: "Post",
url: '/api/API',
data: Students,
datatype: 'json',
cache: false,
})
.success(function (data) {
var info1 = "<hr /><b>The new student is the following</b><br />";
info1 += "<b>Student ID : </b>" + data.StudentID + "<br />";
info1 += "<b>Name : </b>" + data.Name + "<br />";
info1 += "<b>Surname : </b>" + data.Surname + "<hr />";
info1 += "<b>Address : </b>" + data.Address;
info1 += "<b>Field of Study : </b>" + data.Field_of_Study;
info1 += "<b>Year of Study : </b>" + data.Year_of_Study;
info1 += "<b>Date_of_birth : </b>" + data.Date_of_birth;
$('#Emessage').html(info1);
})
.error(function (xhr, ajaxOptions, thrownError) {
$('#Emessage').html("Error : An error occured");
});
return false;
});
$(document).ajaxStart(function () {
$('#loading').show();
});
$(document).ajaxStop(function () {
$('#loading').hide();
});
});
</script>
}
Contorller:
using projektApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
namespace projektApi.Controllers
{
public class APIController : ApiController
{
BazastudEntities db = new BazastudEntities();
public IEnumerable<Students> Get()
{
return db.Students;
}
[ResponseType(typeof(Students))]
public IHttpActionResult Post(Students student)
{
/*int maxId = db.Students.Max(e => e.Id);
db.Students.Add(student);
db.SaveChanges();*/
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Students.Add(student);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = student.Id }, student);
}
}
}