Moortiii

barebones

Mar 16th, 2019
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************************
  2. *** SignupController.cs ***
  3. *********************************/
  4.  
  5. namespace assignment_5.Controllers
  6. {
  7.     public class SignupController : Controller
  8.     {
  9.         public IActionResult Index()
  10.         {
  11.             return View();
  12.         }
  13. }
  14.  
  15. /********************************
  16. *** ParticipantsController.cs ***
  17. *********************************/
  18.  
  19. namespace assignment_5.Controllers
  20. {
  21.     [Route("api/[controller]")]
  22.     [ApiController]
  23.     public class ParticipantsController : ControllerBase
  24.     {
  25.         private readonly ApplicationDbContext _context;
  26.  
  27.         // Dependency Inject Database Context
  28.         public ParticipantsController(ApplicationDbContext context)
  29.         {
  30.             _context = context;
  31.         }
  32.  
  33.         // Grab all participants from the database and return them as a list
  34.         // GET api/Signup
  35.         [HttpGet]
  36.         public async Task<ActionResult<IEnumerable<Participant>>> GetParticipantsAsync()
  37.         {
  38.             return await _context.Participants.ToListAsync();
  39.         }
  40. }
  41.  
  42. /**************************
  43. ****** index.cshtml *******
  44. **************************/
  45.  
  46. <!-- Du trenger kun et element som Vue kan "hekte" seg på. ID == navnet som skal stå i el i site.js -->
  47. div id="app"></div>
  48.  
  49. /**************************
  50. ********* site.js *********
  51. **************************/
  52.  
  53. <script src="...link-til-axios"></script>
  54.  
  55. let url = "/api/participants/";
  56.  
  57. let app = new Vue({
  58.     el: "#app",
  59.     data: {
  60.         participants: []
  61.     },
  62.  
  63.     // Her ser du koss man iterere over data fra APIen. Data feltet vårt har en entry "participants".
  64.     // I metoden, created, som kalles automatisk når vue opprettes, så sørge me for at this.participants
  65.     // mottar data ved å bruke axios.get på URL. Merk at url = /api/participants. Den bruke altså "HTTP GET" metoden
  66.     // i ParticipantsController.
  67.  
  68.     template: `
  69.     <div>
  70.         <ol>
  71.             <li v-for="participant in participants">
  72.                 <p>{{ participant.id }} - {{ participant.name }} - {{ participant.email }} - {{ participant.age }} </p>
  73.             </li>
  74.         </ol>
  75.     </div>
  76.  
  77.     created() {
  78.         axios.get(url).then((response) => {
  79.             this.participants = response.data;
  80.         });
  81.     }
  82. })
Advertisement
Add Comment
Please, Sign In to add comment