Final Project

harleyteam34
IpmplementingControlsAspnetmvccode.docx

Implementing Controls

Jamie Vaughan

Southern New Hampshire University

2 April 2018

MODEL CODE

namespace Italian_bicycle_company.Models

{

using System;

using System.ComponentModel.DataAnnotations;

using System.Data.Entity;

using System.Linq;

public class Person

{

[Required]

[Display(Name = "Business Entity ID")]

public int ID { get; set; }

[Required]

[StringLength(2)]

[Display(Name = "Person Type")]

public string PersonType { get; set; }

[Required]

[Display(Name = "Name Style")]

public int NameStyle { get; set; }

[Required]

[StringLength(8)]

public string Title { get; set; }

[Required]

[StringLength(50)]

[Display(Name = "First Name")]

public string FirstName { get; set; }

[Required]

[StringLength(50)]

[Display(Name = "Middle Name")]

public string MiddleName { get; set; }

[Required]

[StringLength(50)]

[Display(Name = "Last Name")]

public string LastName { get; set; }

[Required]

[StringLength(10)]

public string Suffix { get; set; }

[Required]

[Range(0,2)]

[Display(Name = "Email Promotion")]

public int EmailPromotion { get; set; }

[Required]

[Display(Name = "Additional Contact Info")]

public string AdditionalContactInfo { get; set; }

[Required]

public string Demographics { get; set; }

[Required]

[Display(Name = "Modified Date")]

[DataType(DataType.Date)]

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

public DateTime ModifiedDate { get; set; }

}

public class bicycleModel : DbContext

{

public bicycleModel()

: base("name=bicycleModel")

{

}

public System.Data.Entity.DbSet<Italian_bicycle_company.Models.Person> People { get; set; }

}

}

VIEW CODE

@model Italian_bicycle_company.Models.Person

@{

ViewBag.Title = "Create";

}

<h2>Create</h2>

@using (Html.BeginForm())

{

@Html.AntiForgeryToken()

<div class="form-horizontal">

<h4>Person</h4>

<hr />

@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">

@Html.LabelFor(model => model.PersonType, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.PersonType, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.PersonType, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.NameStyle, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.NameStyle, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.NameStyle, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.Suffix, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.Suffix, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.Suffix, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.EmailPromotion, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.EmailPromotion, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.EmailPromotion, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.AdditionalContactInfo, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.AdditionalContactInfo, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.AdditionalContactInfo, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.Demographics, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.Demographics, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.Demographics, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

@Html.LabelFor(model => model.ModifiedDate, htmlAttributes: new { @class = "control-label col-md-2" })

<div class="col-md-10">

@Html.EditorFor(model => model.ModifiedDate, new { htmlAttributes = new { @class = "form-control" } })

@Html.ValidationMessageFor(model => model.ModifiedDate, "", new { @class = "text-danger" })

</div>

</div>

<div class="form-group">

<div class="col-md-offset-2 col-md-10">

<input type="submit" value="Create" class="btn btn-default" />

</div>

</div>

</div>

}

<div>

@*@Html.ActionLink("Back to List", "Index")*@

</div>

@section Scripts {

@Scripts.Render("~/bundles/jqueryval")

}

CONTROLLER CODE

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;

namespace Italian_bicycle_company.Models

{

public class PeopleController : Controller

{

private bicycleModel db = new bicycleModel();

// GET: People

public ActionResult Index()

{

return View(db.People.ToList());

}

// GET: People/Details/5

public ActionResult Details(int? id)

{

if (id == null)

{

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

}

Person person = db.People.Find(id);

if (person == null)

{

return HttpNotFound();

}

return View(person);

}

// GET: People/Create

public ActionResult Create()

{

return View();

}

// POST: People/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 = "ID,PersonType,NameStyle,Title,FirstName,MiddleName,LastName,Suffix,EmailPromotion,AdditionalContactInfo,Demographics,ModifiedDate")] Person person)

{

if (ModelState.IsValid)

{

db.People.Add(person);

db.SaveChanges();

return RedirectToAction("Index");

}

return View(person);

}

// GET: People/Edit/5

public ActionResult Edit(int? id)

{

if (id == null)

{

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

}

Person person = db.People.Find(id);

if (person == null)

{

return HttpNotFound();

}

return View(person);

}

// POST: People/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 = "ID,PersonType,NameStyle,Title,FirstName,MiddleName,LastName,Suffix,EmailPromotion,AdditionalContactInfo,Demographics,ModifiedDate")] Person person)

{

if (ModelState.IsValid)

{

db.Entry(person).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Index");

}

return View(person);

}

// GET: People/Delete/5

public ActionResult Delete(int? id)

{

if (id == null)

{

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

}

Person person = db.People.Find(id);

if (person == null)

{

return HttpNotFound();

}

return View(person);

}

// POST: People/Delete/5

[HttpPost, ActionName("Delete")]

[ValidateAntiForgeryToken]

public ActionResult DeleteConfirmed(int id)

{

Person person = db.People.Find(id);

db.People.Remove(person);

db.SaveChanges();

return RedirectToAction("Index");

}

protected override void Dispose(bool disposing)

{

if (disposing)

{

db.Dispose();

}

base.Dispose(disposing);

}

}

}