Sunday 22 January 2017

MVC model








//CustomerController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CPRG102.Mod4.ModelDemo.Models;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace CPRG102.Mod4.ModelDemo.Controllers
{
    public class CustomerController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            var customers = CustomerManager.Customers;
            return View(customers);  //customers from model/CustomerManager/list<customers>
        }

        //the GET method:  Customer/Create
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Customer customer)
        {
            //add the customer to the system
            CustomerManager.Add(customer);
            return RedirectToAction("Index"); //add a new customer, display table again
        }
    }
}

-----------------------------------------------------------------

//~/views/customer/index.cshtml

@model List<CPRG102.Mod4.ModelDemo.Models.Customer>

@*
    <ul>
        @foreach (var cust in Model)
        {
            <li>
                @cust.ToString()
            </li>
        }
    </ul>
*@ 
<h4>Customer Lookup</h4>

<table class="table">
    <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Phone</th>
    </tr>
    @foreach (var cust in Model)
    {
        <tr>
            <td>@cust.Id</td>
            <td>@cust.FirstName</td>
            <td>@cust.LastName</td>
            <td>@cust.Phone</td>
        </tr>
    }
</table>

<a href="~/Customer/Create">Create New Customer</a>

--------------------------------------------------------------------

//~/views/customer/Create.cshtml

<h4>Add Customer</h4>
<!-- Post back to the same controller method-->
<form method="post">
<table class="table">
    <tr>
        <td>ID:</td>
        <td><input type="text" name="Id" /></td>
    </tr>
    <tr>
        <td>First Name:</td>
        <td><input type="text" name="FirstName" /></td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td><input type="text" name="LastName" /></td>
    </tr>
    <tr>
        <td>Phone:</td>
        <td><input type="text" name="Phone" /></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="Submit" /></td>
    </tr>
</table>
</form>

------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CPRG102.Mod4.ModelDemo.Models
{
    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }

        public override string ToString()
        {
            return $"{FirstName} {LastName}";
        }
    }
}

--------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CPRG102.Mod4.ModelDemo.Models
{
    public class CustomerManager
    {
        public static List<Customer> Customers { get; private set; }

        static CustomerManager()
        {
            Customers = new List<Customer>();
            Customers.Add(new Customer { Id = 1, FirstName = "John", LastName = "Doe", Phone = "403-555-5670" });
            Customers.Add(new Customer { Id = 2, FirstName = "Jane", LastName = "Smith", Phone = "403-555-1234" });
            Customers.Add(new Customer { Id = 3, FirstName = "Ken", LastName = "Hunter", Phone = "403-555-9950" });
        }

        public static void Add(Customer customer)
        {
            Customers.Add(customer);
        }

        public static Customer Find(int id)
        {
            var cust = Customers.SingleOrDefault(c => c.Id == id);
            return cust;
        }

    }
}

No comments:

Post a Comment