There is an asp.net web api project. In which there is an apicontroller.
// GET api/values public IQueryable<Contact> Get() { return repository.Contacts; } // GET api/values/5 public Contact Get(string id) { var tempcontact = repository.SearchContact(id); return tempcontact; } // POST api/values [HttpPost] public void Post(Contact value) { if (ModelState.IsValid && value!=null) { repository.SaveContact(value); } } // PUT api/values/5 [HttpPut] public void Put(int id, Contact value) { if (ModelState.IsValid) { repository.SaveContact(value); } } // DELETE api/values/5 [HttpDelete] public void Delete(int id) { repository.DeleteContact(id); } } There is also a View and javascript functions that access api.
$(document).ready(function () { $.ajax({ url: 'http://localhost:55285/api/values/', type: 'Get', dataType: 'json', success: function (dataContacts) { WriteResponse(dataContacts); }, error: function (x, y, z) { alert(x + '\n' + y + '\n' + z); } }); }); The same function draws a table in which there are buttons. And these buttons do not work. These buttons also have js functions that are called when pressed. If you create buttons not js, but in view, then everything works. The database has about 500+ records and a button is needed for each record. Which way to go and what to look for? Google can not help because I do not know what to look for.
function WriteResponse(contacts) { var strResult = ""; var iio = 0; $.each(contacts, function (index, contact) { strResult += "<div class=\"row\">"; strResult += "<input class=\"form-control\" value=\"" + contact.ContactID + "\"/><input class=\"form-control\" value=\"" + contact.first_name + "\"/> strResult += "<div class=\"col-md-1\"><button id=\"DeleteBtn\" data-action=\"Delete\" class=\"btn btn-default\"><span class=\"glyphicon glyphicon-floppy-disk\"></span></button><button id=\"SaveBtn\" data-action=\"Save\" class=\"btn btn-default\"><span class=\"glyphicon glyphicon glyphicon-remove\"></span></button></div></div>"; }); $("#divResult").html(strResult); } One of the handlers.
$(document).ready(function () { jQuery.support.cors = true; $("#SaveBtn").click(function () { var contact = { first_name: $("#first_name").val(), }; $.ajax({ url: 'http://localhost:55285/api/values/', type: 'POST', data: JSON.stringify(contact), contentType: 'application/json; charset=utf-8', success: function (data) { }, error: function (x, y, z) { alert(x + '\n' + y + '\n' + z); } }); }); });
http://localhost:55285/api/values/delete/5there is a deletion, and whenhttp://localhost:55285/api/values/get/5tossed to the edit page. - MasterAlex