0

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);
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Waq
  • 29
  • 9
  • This [post](http://stackoverflow.com/questions/7351206/custom-validation-error-message-if-user-puts-a-non-numeric-string-in-an-int-fiel?lq=1) and its links explain _why_ and what you can do to change the message. – Jasen Aug 14 '14 at 03:53

1 Answers1

1

Non-nullable scalar values such as int,DateTime,decimal etc are always considered as required because you can not insert null into it. so if you don't want to validate these field then use int? for a non-required int.

     public int? GenreId { get; set; }
     public int? ArtistId { get; set; }

No need to do this on string data type since string is nullable.

Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25
  • I need the validation, but I don't understand why the message automatically created for the 2 drop-down lists. 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); }' – Waq Aug 14 '14 at 03:21
  • As i said int is automatically considered as required. don't matter you define it required or not. – Manish Parakhiya Aug 14 '14 at 03:23
  • Ok, I'll forget it. I tried to add "?" after "int", and it does skip the validation check. I also changed the [DisplayName("Genre")] to other names, so if i use the validation, the name of the error message changes as the name changes. – Waq Aug 14 '14 at 05:33