I just started to learn C# and SQL and am now learning MVC3
. I downloaded the free ASP.NET MVC Music Store Tutorial - Version 3.0b from http://mvcmusicstore.codeplex.com/ which is a 135-page file. When I read page 76, it is about doing Model validation using Data Annotation
attributes. This is the code:
namespace MvcMusicStore.Models
{
[Bind(Exclude = "AlbumId")]
public class Album
{
[ScaffoldColumn(false)]
public int AlbumId { get; set; }
[DisplayName("Genre")]
public int GenreId { get; set; }
[DisplayName("Artist")]
public int ArtistId { get; set; }
[Required(ErrorMessage = "An Album Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and 100.00")]
public decimal Price { get; set; }
[DisplayName("Album Art URL")]
[StringLength(1024)]
public string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
}
After pasting the code in the solution, I can see that the validation works, but there's one thing I don't understand: There is no "required" tag for the dropdown lists of Genre and Artist; if I choose nothing, and click the save button, the following error message is shown: "The Genre field is required" and "The Artist field is required"
I went back to previous pages and found that it says "Validation errors are automatically displayed using the @HTML.ValidationMessageFor HTML Helper
" on page 65, but when I search the whole solution for the two sentences I get no results.
Can anyone tell where are the definitions of the two error messages?
In the source code, the drop-down lists are defined as follows: // // POST: /StoreManager/Edit/5
[HttpPost]
public ActionResult Edit(Album album)
{
if (ModelState.IsValid)
{
db.Entry(album).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}