Enabling code to do drop down lists in music store.

This commit is contained in:
Praburaj 2014-04-14 17:26:39 -07:00
parent 24293783f5
commit f95588f0dd
3 changed files with 50 additions and 40 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using MusicStore.Models;
using System.Linq;
@ -54,8 +55,8 @@ namespace MusicStore.Controllers
public IActionResult Create()
{
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
return View();
}
@ -70,11 +71,11 @@ namespace MusicStore.Controllers
db.SaveChanges();
//Bug: RedirectToAction() not available
//return RedirectToAction("Index");
return View();
return Redirect("/");
}
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}
@ -91,8 +92,8 @@ namespace MusicStore.Controllers
//return HttpNotFound();
return new HttpStatusCodeResult(404);
}
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}
@ -107,10 +108,10 @@ namespace MusicStore.Controllers
db.SaveChanges();
//Bug: Missing RedirectToAction helper
//return RedirectToAction("Index");
return View();
return Redirect("/");
}
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}
@ -140,8 +141,7 @@ namespace MusicStore.Controllers
db.SaveChanges();
//Bug: Missing helper
//return RedirectToAction("Index");
return View();
return Redirect("/");
}
//Bug: Can't dispose db.

View File

@ -11,7 +11,7 @@
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@*@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>Album</h4>
@ -19,42 +19,47 @@
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.GenreId, "GenreId", new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.GenreId, "GenreId", new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">GenreId</label>
<div class="col-md-10">
@Html.DropDownList("GenreId", String.Empty)
@Html.ValidationMessageFor(model => model.GenreId)
@*@Html.ValidationMessageFor(model => model.GenreId)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ArtistId, "ArtistId", new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.ArtistId, "ArtistId", new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">ArtistId</label>
<div class="col-md-10">
@Html.DropDownList("ArtistId", String.Empty)
@Html.ValidationMessageFor(model => model.ArtistId)
@*@Html.ValidationMessageFor(model => model.ArtistId)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">Title</label>
<div class="col-md-10">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
@*@Html.EditorFor(model => model.Title)*@
@*@Html.ValidationMessageFor(model => model.Title)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">Price</label>
<div class="col-md-10">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
@*@Html.EditorFor(model => model.Price)*@
@*@Html.ValidationMessageFor(model => model.Price)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AlbumArtUrl, new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.AlbumArtUrl, new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">AlbumArtUrl</label>
<div class="col-md-10">
@Html.EditorFor(model => model.AlbumArtUrl)
@Html.ValidationMessageFor(model => model.AlbumArtUrl)
@*@Html.EditorFor(model => model.AlbumArtUrl)*@
@*@Html.ValidationMessageFor(model => model.AlbumArtUrl)*@
</div>
</div>

View File

@ -10,7 +10,7 @@
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@*@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>Album</h4>
@ -19,42 +19,47 @@
@Html.HiddenFor(model => model.AlbumId)
<div class="form-group">
@Html.LabelFor(model => model.GenreId, "GenreId", new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.GenreId, "GenreId", new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">Genre Id</label>
<div class="col-md-10">
@Html.DropDownList("GenreId", String.Empty)
@Html.ValidationMessageFor(model => model.GenreId)
@*@Html.ValidationMessageFor(model => model.GenreId)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ArtistId, "ArtistId", new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.ArtistId, "ArtistId", new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">ArtistId</label>
<div class="col-md-10">
@Html.DropDownList("ArtistId", String.Empty)
@Html.ValidationMessageFor(model => model.ArtistId)
@*@Html.ValidationMessageFor(model => model.ArtistId)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">Title</label>
<div class="col-md-10">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
@*@Html.EditorFor(model => model.Title)*@
@*@Html.ValidationMessageFor(model => model.Title)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">Price</label>
<div class="col-md-10">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
@*@Html.EditorFor(model => model.Price)*@
@*@Html.ValidationMessageFor(model => model.Price)*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AlbumArtUrl, new { @class = "control-label col-md-2" })
@*@Html.LabelFor(model => model.AlbumArtUrl, new { @class = "control-label col-md-2" })*@
<label class="control-label col-md-2">albumArtUrl</label>
<div class="col-md-10">
@Html.EditorFor(model => model.AlbumArtUrl)
@Html.ValidationMessageFor(model => model.AlbumArtUrl)
@*@Html.EditorFor(model => model.AlbumArtUrl)*@
@*@Html.ValidationMessageFor(model => model.AlbumArtUrl)*@
</div>
</div>