| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Get control of your email attachments. Connect all your Gmail accounts and in less than 2 minutes, Dokkio will automatically organize your file attachments. You can also connect Dokkio to Drive, Dropbox, and Slack. Sign up for free.

View
 

Lecture - Menu Case Study Part 2 - Creating a Custom View

Page history last edited by Dr. Ron Eaglin 4 years, 4 months ago

 

Lecture - Menu Case Study Part 2 - Creating a Custom View

 

 

 

Prerequisites

 

Lecture - Menu Case Study Part 1 - Using MVC to Create Entry Screens

 

This is part of Case Study - Menu Example

 

Summary

 

By simply adding a method to an existing controller and a new custom view copied from an existing view, we can create a custom view for our menu project.

 

Video 

 

 

Reference Materials

 

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using COP4709MenuDatabase.Models;

namespace COP4709MenuDatabase.Controllers
{
    public class MenusController : Controller
    {
        private COP4709MenuDatabaseContext db = new COP4709MenuDatabaseContext();

        // GET: Menus
        public ActionResult Index()
        {
            return View(db.Menus.ToList());
        }

        // GET: Menus/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Menu menu = db.Menus.Find(id);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }

        public ActionResult ViewMenu(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Menu menu = db.Menus.Find(id);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }

        // GET: Menus/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Menus/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "MenuID,MenuTitle,MenuDescription")] Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Menus.Add(menu);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(menu);
        }

        // GET: Menus/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Menu menu = db.Menus.Find(id);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }

        // POST: Menus/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "MenuID,MenuTitle,MenuDescription")] Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(menu).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(menu);
        }

        // GET: Menus/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Menu menu = db.Menus.Find(id);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }

        // POST: Menus/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Menu menu = db.Menus.Find(id);
            db.Menus.Remove(menu);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

 
@model COP4709MenuDatabase.Models.Menu

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Menu</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.MenuTitle)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MenuTitle)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.MenuDescription)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MenuDescription)
        </dd>

    </dl>
    
    <table>
        <tr>
            <th>Item</th>
            <th>Description</th>
            <th>Cost</th>
        </tr>

        @foreach (var item in Model.MenuItems)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.MenuItemTitle)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.MenuItemDescription )
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.MenuItemCost )
                </td>

            </tr>
        }
    </table>

</div>

<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.MenuID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

 

 

Menu Case Study Part 3 - 

 

Menu Case Study Part 4 - Generating a Javascript call from a Razor Object

 

 

Additional Information

 

 

COP 4834 Lectures Page

Comments (0)

You don't have permission to comment on this page.