finish crud

This commit is contained in:
Mateusz Kaczor 2019-05-14 00:04:52 +02:00
parent 059e20d4fb
commit dc8d3a2b34
9 changed files with 202 additions and 3 deletions

View File

@ -80,5 +80,86 @@ namespace MovieBase.Controllers
return View(movie);
}
//GET: Movie/Edit/guid
public ActionResult Edit(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
// POST: Student/Edit/5
[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public ActionResult EditPost(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var movieToUpdate = db.Movies.Find(id);
if (TryUpdateModel(movieToUpdate, "",
new string[] { "Title", "Plot", "WatchedDate" }))
{
try
{
db.SaveChanges();
return RedirectToAction("Index");
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
}
return View(movieToUpdate);
}
// GET: Movie/Delete/5
public ActionResult Delete(Guid? id, bool? saveChangesError = false)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (saveChangesError.GetValueOrDefault())
{
ViewBag.ErrorMessage = "Delete failed. Try again, and if the problem persists see your system administrator.";
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
// POST: Student/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(Guid? id)
{
try
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
}
catch (RetryLimitExceededException/* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
return RedirectToAction("Delete", new { id = id, saveChangesError = true });
}
return RedirectToAction("Index");
}
}
}

View File

@ -35,6 +35,7 @@ namespace MovieBase.Controllers
HttpResponseMessage responseMessage = await client.GetAsync(String.Format(@"http://www.omdbapi.com/?t={0}&apikey={1}", title, apiKey));
Movie movie = JsonConvert.DeserializeObject<Movie>(await responseMessage.Content.ReadAsStringAsync());
movie.WatchedDate = DateTime.Now.Date;
if (string.IsNullOrEmpty(movie.Title))
{
return PartialView();

View File

@ -16,6 +16,9 @@ namespace MovieBase.Models
public string Poster { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Watched Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime WatchedDate { get; set; }
//public List<Actor> StarringActors { get; set; }

View File

@ -306,6 +306,8 @@
<Content Include="Views\Movie\Create.cshtml" />
<Content Include="Views\Movie\Details.cshtml" />
<Content Include="Views\Shared\_Details.cshtml" />
<Content Include="Views\Movie\Edit.cshtml" />
<Content Include="Views\Movie\Delete.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View File

@ -33,7 +33,8 @@
<div class="form-group">
@Html.LabelFor(model => model.WatchedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WatchedDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.WatchedDate, "{0:yyyy-MM-dd}", new { htmlAttributes = new { @class = "form-control", @type ="date" } })
@Html.TextBoxFor(m => m.WatchedDate, "{0:yyyy-MM-dd}", new { htmlattributes = new { @class = "form-control" }, @type = "date" })
@Html.ValidationMessageFor(model => model.WatchedDate, "", new { @class = "text-danger" })
</div>
</div>

View File

@ -0,0 +1,49 @@
@model MovieBase.Models.Movie
@{
ViewBag.Title = "Delete";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Delete</h2>
<p class="error">@ViewBag.ErrorMessage</p>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Plot)
</dt>
<dd>
@Html.DisplayFor(model => model.Plot)
</dd>
<dt>
@Html.DisplayNameFor(model => model.WatchedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.WatchedDate)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>

View File

@ -0,0 +1,59 @@
@model MovieBase.Models.Movie
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.MovieId)
<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.Plot, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Plot, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Plot, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WatchedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WatchedDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WatchedDate, "", 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>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

View File

@ -34,6 +34,8 @@
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = movie.MovieId }) |
@Html.ActionLink("Edit", "Edit", new { id = movie.MovieId }) |
@Html.ActionLink("Delete","Delete", new {id = movie.MovieId })
</td>
</tr>
}

View File

@ -22,9 +22,10 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
@*<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>*@
<li>@Html.ActionLink("Movie", "Index", "Movie")</li>
<li>@Html.ActionLink("Search", "Index", "Search")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>