P. 1
MVC Music Store - Tutorial - V2.0

MVC Music Store - Tutorial - V2.0

|Views: 13|Likes:
Published by kingshuk_paul

More info:

Published by: kingshuk_paul on Jun 04, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





We will make use of another HTML Helper to create our dropdowns, Html.DropDownList. Let’s look at the
information we need to pass for the Artist dropdown:

The name of the form field (ArtistId)
The list of values for the dropdown, passed as a SelectList
The Data Value field which should be posted back with the form
The Data Text field which should be displayed in the dropdown list
The Selected Value which is used to set the dropdown list value when the form is displayed

Here’s what our call looks like:

new SelectList(ViewBag.Artists as System.Collections.IEnumerable,
"ArtistId", "Name", Model.ArtistId))

Our completed Album.ascx editor template code appears as follows:

@model MvcMusicStore.Models.Album

@Html.LabelFor(model => model.Genre)
new SelectList(ViewBag.Genres as System.Collections.IEnumerable,
"GenreId", "Name", Model.GenreId))

@Html.LabelFor(model => model.Artist)
new SelectList(ViewBag.Artists as System.Collections.IEnumerable,
"ArtistId", "Name", Model.ArtistId))

@Html.LabelFor(model => model.Title)
@Html.TextBoxFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)

@Html.LabelFor(model => model.Price)
@Html.TextBoxFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)

MVC Music Store Tutorial v2.0 – http://mvcmusicstore.codeplex.com – Tutorial under Creative Commons Attribution 3.0 License
Page 74

@Html.LabelFor(model => model.AlbumArtUrl)
@Html.TextBoxFor(model => model.AlbumArtUrl)
@Html.ValidationMessageFor(model => model.AlbumArtUrl)

Now when we edit an album from within our Store Manager, we see dropdowns instead of Artist and Genre
ID text fields.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->