Based on this tutorial but modified not to use Entity Framework, this example shows a simple ASP.NET MVC4 app.
Create /Permits/Create
Edit /Permits/Edit/1
Details /Permits/Details/1
Delete /Permits/Delete/1
Model
using System.ComponentModel.DataAnnotations; namespace MvcApplication6.Models { public class Permit { [Required] public int Id { get; set; } public string Area { get; set; } } }
Controller
using System.Web.Mvc; using MvcApplication6.Models; using MvcApplication6.Repos; namespace MvcApplication6.Controllers { public class PermitsController : Controller { private PermitRepository repos = new PermitRepository(); // // GET: /Permits/ public ActionResult Index() { return View(repos.Permits); } // // GET: /Permits/Details/5 public ActionResult Details(int id = 0) { Permit permit = repos.FindPermit(id); if (permit == null) { return HttpNotFound(); } return View(permit); } // // GET: /Permits/Create public ActionResult Create() { return View(); } // // POST: /Permits/Create [HttpPost] public ActionResult Create(Permit permit) { if (ModelState.IsValid) { repos.AddPermit(permit); return RedirectToAction("Index"); } return View(permit); } // // GET: /Permits/Edit/5 public ActionResult Edit(int id = 0) { Permit permit = repos.FindPermit(id); if (permit == null) { return HttpNotFound(); } return View(permit); } // // POST: /Permits/Edit/5 [HttpPost] public ActionResult Edit(Permit permit) { if (ModelState.IsValid) { repos.UpdatePermit(permit); return RedirectToAction("Index"); } return View(permit); } // // GET: /Movies/Delete/5 public ActionResult Delete(int id = 0) { Permit permit = repos.FindPermit(id); if (permit == null) { return HttpNotFound(); } return View(permit); } // // POST: /Movies/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Permit permit = repos.FindPermit(id); repos.DeletePermit(permit); return RedirectToAction("Index"); } } }
View
Index.cshtml
@model IEnumerable @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Permit Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> @Html.DisplayNameFor(model => model.Id) </th> <th> @Html.DisplayNameFor(model => model.Area) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Id) </td> <td> @Html.DisplayFor(modelItem => item.Area) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) </td> </tr> } </table>
Details.cshtml
@model MvcApplication6.Models.Permit @{ ViewBag.Title = "Details"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Details</h2> <fieldset> <legend>Permit</legend> <div class="display-label"> @Html.DisplayNameFor(model => model.Id) </div> <div class="display-field"> @Html.DisplayFor(model => model.Id) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.Area) </div> <div class="display-field"> @Html.DisplayFor(model => model.Area) </div> </fieldset> <p> @Html.ActionLink("Edit", "Edit", new { id=Model.Id }) | @Html.ActionLink("Back to List", "Index") </p>
Create.cshtml
@model MvcApplication6.Models.Permit @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Permit</legend> <div class="editor-label"> @Html.LabelFor(model => model.Id) </div> <div class="editor-field"> @Html.EditorFor(model => model.Id) @Html.ValidationMessageFor(model => model.Id) </div> <div class="editor-label"> @Html.LabelFor(model => model.Area) </div> <div class="editor-field"> @Html.EditorFor(model => model.Area) @Html.ValidationMessageFor(model => model.Area) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Edit.cshtml
@model MvcApplication6.Models.Permit @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Edit</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Permit</legend> @Html.HiddenFor(model => model.Id) <div class="editor-label"> @Html.LabelFor(model => model.Id) </div> <div class="editor-field"> @Html.EditorFor(model => model.Id) @Html.ValidationMessageFor(model => model.Id) </div> <div class="editor-label"> @Html.LabelFor(model => model.Area) </div> <div class="editor-field"> @Html.EditorFor(model => model.Area) @Html.ValidationMessageFor(model => model.Area) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Delete.cshtml
@model MvcApplication6.Models.Permit @{ ViewBag.Title = "Delete"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Delete</h2> <h3>Are you sure you want to delete this?</h3> <fieldset> <legend>Permit</legend> <div class="display-label"> @Html.DisplayNameFor(model => model.Id) </div> <div class="display-field"> @Html.DisplayFor(model => model.Id) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.Area) </div> <div class="display-field"> @Html.DisplayFor(model => model.Area) </div> </fieldset> @using (Html.BeginForm()) { <p> <input type="submit" value="Delete" /> | @Html.ActionLink("Back to List", "Index") </p> }
R.Monika
May 29, 2013 @ 05:18:23
Thank u Bos………It is very useful to me……..& i want Demo for this…..
R.Monika
May 29, 2013 @ 09:44:52
Hi………..In this code,the actions Delete,Create is not perfrmed….I use the grid…how to refresh….Can anyone help to me…
Royce Roy
Jun 06, 2013 @ 14:33:53
This is awesome!! really helpful for me. Thanks for sharing with us. Following links also helped me to complete my task.
http://www.mindstick.com/Articles/63bce1d2-3d08-48a1-a325-57754b1f000e/?Creating%20a%20simple%20model%20using%20ASP%20NET%20MVC4
http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model
ToddW
Aug 26, 2013 @ 18:28:25
This is nice short and sweet example. Question though, what’s the best way to add an ActionLink to the Area Info so that it generates a seperate view of additional “area” related info fields?
fanfarron
Feb 11, 2014 @ 21:38:35
I need to set one of the fields to a known value. That is, I do not need the user to specify the value associated w/a particular field. How do I do this?
Example : currently I have this in place :
@Html.EditorFor(Function(Model) Model.ProjectID)
I already know the value that should go in the “ProjectID” field of the Model. So how do I set it in the View code?
Chintan
Mar 06, 2014 @ 06:35:08
Make it a hidden field. If it is in the model, You can get the value from Action method by passing an model object as parameter or you can retrieve it by Request.Form[“FieldName”] in the Action.
jeremy
Jul 01, 2014 @ 05:27:50
Do we need to separate class for mvcapplication6.repos?..Can you please give me a detailed explanation about the repos definition?
Sagar
Sep 05, 2014 @ 07:40:52
Nice Article
anthony
Dec 08, 2017 @ 12:05:30
Good one but not provided ‘PermitRepository’. not complete one.