sainture
3/27/2019 - 4:40 AM

Controllers basic example

// first create a Controllers folder

// basic example of a controller and its actions

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Controllers
{
    public class ConferenceController: Controller
    {
        private readonly IConferenceService conferenceService;

        // constructor
        public ConferenceController(IConferenceService conferenceService)
        {
            // dependency injection is similar to Angular except here you need to declare service reference as a private property
            this.conferenceService = conferenceService;
        }
        public async Task<IActionResult> Index()
        {
            ViewBag.Title = "Conference Overview";
            return View(await conferenceService.GetAll());
        }

        public IActionResult Add()
        {
            ViewBag.Title = "Add Conference";
            return View(new ConferenceModel());
        }

        [HttpPost]
        public async Task<IActionResult> Add(ConferenceModel model)
        {
            if (ModelState.IsValid)
                await conferenceService.Add(model);

            return RedirectToAction("Index");
        }
    }
}