I'm trying to create a full CRUD application on MVC without a database. I used the template view creator for "Create/Details/Edit/Delete..." I'm having trouble saving/editing/delete since most resources online work off of a database/framework. For "Create", after inputting in all the fields, it bring me back to the "List" page of all PersonModel without the newly input PersonModel. Am unable to get [HttpPost] to work for edit.
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers
{
public class PersonController : Controller
{
List<PersonModel> people = new List<PersonModel>()
{
new PersonModel { First = "DAD", Last = "Nguyen", Name = "Study", About = "Study for test", Done = false, Id = 1 },
new PersonModel { First = "David", Last = "Smith", Name = "Grocery", About = "Buy apple and bananana", Done = true , Id = 2},
new PersonModel { First = "Tom", Last = "Davidson", Name = "Fix car", About = "Headlight and mirror is broken", Done = false, Id = 3 },
new PersonModel { First = "Maddie", Last = "Madison", Name = "Job application", About = "Follow up on job interview ", Done = false , Id = 4}
};
// GET: Person
public ActionResult Index()
{
return View(people);
}
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(PersonModel per)
{
try
{
if (!ModelState.IsValid)
{
return View("Create", per);
}
people.Add(per);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Details(int id)
{
PersonModel per = people.Find(emp => emp.Id == id);
return View(per);
}
[HttpGet]
public ActionResult Edit(int id)
{
PersonModel per = people.Find(emp => emp.Id == id);
return View(per);
}
public ActionResult Delete()
{
return View();
}
}
}
Thank you.
Edit
Class model
public class PersonModel
{
public int Id { get; set; }
public string First { get; set; }
public string Last { get; set; }
public bool Done { get; set; }
public string Name { get; set; }
public string About { get; set; }
}
Index cshtml
@model IEnumerable<MVCDemo.Models.PersonModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First)
</th>
<th>
@Html.DisplayNameFor(model => model.Last)
</th>
<th>
@Html.DisplayNameFor(model => model.Done)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.About)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.First)
</td>
<td>
@Html.DisplayFor(modelItem => item.Last)
</td>
<td>
@Html.DisplayFor(modelItem => item.Done)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.About)
</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>
Create cshtml
@model MVCDemo.Models.PersonModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PersonModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.First, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.First, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.First, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Last, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Last, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Last, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Done, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Done)
@Html.ValidationMessageFor(model => model.Done, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.About, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.About, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.About, "", 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>
Details cshtml
@model MVCDemo.Models.PersonModel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>PersonModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.First)
</dt>
<dd>
@Html.DisplayFor(model => model.First)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Last)
</dt>
<dd>
@Html.DisplayFor(model => model.Last)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Done)
</dt>
<dd>
@Html.DisplayFor(model => model.Done)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.About)
</dt>
<dd>
@Html.DisplayFor(model => model.About)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>
Edit cshtml
@model MVCDemo.Models.PersonModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PersonModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.First, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.First, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.First, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Last, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Last, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Last, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Done, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Done)
@Html.ValidationMessageFor(model => model.Done, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.About, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.About, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.About, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>