私は現在asp.netmvcを勉強していて、始めたばかりで、Webフォームからmvcに移行することにしました。
私はこのコードを持っているので興味があります。モデルをreturn View(data)
渡す場合と渡さない場合の違いを知りたいです。
コードは次のとおりです。
コントローラー
/* Even if I comment/remove the lines ViewBag.GenreId....... and ViewBag.ArtistId
and just return View(); everything seems to work fine. I'm following this music store tutorial from codeplex
*/
[HttpPost]
public ActionResult Create(Album album)
{
if (ModelState.IsValid)
{
db.Albums.Add(album);
db.SaveChanges();
return RedirectToAction("Index");
}
//this will assign the values of the dropdownlist of the View
//it will assign the values on the dropdownlist that has a name GenreId
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}
ビューのコード
@model CodeplexMvcMusicStore.Models.Album
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Album</legend>
<div class="editor-label">
@Html.LabelFor(model => model.GenreId, "Genre")
</div>
<div class="editor-field">
@Html.DropDownList("GenreId", String.Empty)
@Html.ValidationMessageFor(model => model.GenreId)
</div>
View(album)
また、オブジェクトモデルを渡す場合と渡さない場合の違いについても知りたいと思いますView()
。