在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
服务定义:
public class ContactsController : ApiController { private static List<Contact> contacts = new List<Contact> { new Contact{ Id="001", Name="张三", PhoneNo="123", EmailAddress="[email protected]" }, new Contact{ Id="002", Name="李四", PhoneNo="456", EmailAddress="[email protected]" } }; // GET api/contacts public IEnumerable<Contact> Get() { return contacts; } // GET api/contacts/5 public Contact Get(string id) { return contacts.FirstOrDefault(c => c.Id == id); } // POST api/contacts public void Post(Contact contact) { Delete(contact.Id); contacts.Add(contact); } // PUT api/contacts/5 public void Put(Contact contact) { contact.Id = Guid.NewGuid().ToString(); contacts.Add(contact); } // DELETE api/contacts/5 public void Delete(string id) { Contact contact = contacts.FirstOrDefault(c => c.Id == id); contacts.Remove(contact); } } 服务调用:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>联系人列表</title> 5 <script src="../Scripts/jquery-1.7.1.js" type="text/javascript"></script> 6 <script src="../Scripts/knockout-2.1.0.debug.js" type="text/javascript"></script> 7 </head> 8 <body> 9 <div id="contacts"> 10 <table> 11 <tr> 12 <th> 13 姓名 14 </th> 15 <th> 16 电话号码 17 </th> 18 <th> 19 Email地址 20 </th> 21 </tr> 22 <tbody> 23 <!-- ko foreach: allContacts --> 24 <tr> 25 <td data-bind="text:Name"> 26 </td> 27 <td data-bind="text:PhoneNo"> 28 </td> 29 <td> 30 <input type="text" class="textbox long" data-bind="value:EmailAddress" /> 31 </td> 32 <td> 33 <a href="#" data-bind="click:$root.updateContact">修改</a> <a href="#" data-bind="click:$root.deleteContact"> 34 删除</a> 35 </td> 36 </tr> 37 <!-- /ko --> 38 <tr data-bind="with:addedContact"> 39 <td> 40 <input type="text" class="textbox" data-bind="value:Name" /> 41 </td> 42 <td> 43 <input type="text" class="textbox" data-bind="value:PhoneNo" /> 44 </td> 45 <td> 46 <input type="text" class="textbox long" data-bind="value:EmailAddress" /> 47 </td> 48 <td> 49 <a href="#" data-bind="click:$root.addContact">添加</a> 50 </td> 51 </tr> 52 </tbody> 53 </table> 54 </div> 55 <script type="text/javascript"> 56 function ContactViewModel() { 57 self = this; 58 self.allContacts = ko.observableArray(); 59 self.addedContact = ko.observable(); 60 61 //加载联系人列表 62 self.loadContacts = function () { 63 $.get("/api/contacts", null, function (data) { 64 self.allContacts(data); 65 var emptyContact = { Id: "", Name: "", PhoneNo: "", EmailAddress: "" }; 66 self.addedContact(emptyContact); 67 }); 68 }; 69 70 //添加联系人 71 self.addContact = function (data) { 72 if (!self.validate(data)) { 73 return; 74 } 75 $.ajax({ 76 url: "/api/contacts/", 77 data: self.addedContact(), 78 type: "PUT", 79 success: self.loadContacts 80 }); 81 }; 82 83 //修改联系人信息 84 self.updateContact = function (data) { 85 $.ajax({ 86 url: "/api/contacts/", 87 data: data, 88 type: "POST", 89 success: self.loadContacts 90 }); 91 }; 92 93 //删除联系人 94 self.deleteContact = function (data) { 95 $.ajax({ 96 url: "/api/contacts/" + data.Id, 97 type: "DELETE", 98 success: self.loadContacts 99 }); 100 }; 101 102 self.validate = function (data) { 103 if (data.Name && data.PhoneNo && data.EmailAddress) { 104 return true; 105 } 106 alert("请输入完整联系人信息!"); 107 return false; 108 } 109 self.loadContacts(); 110 } 111 ko.applyBindings(new ContactViewModel()); 112 </script> 113 </body> 114 </html>
|
请发表评论