简体   繁体   中英

MVC3 Dropdownlist null in post

I have been working on this for several days and haven't got anywhere. My Movies model keeps failing in my post, and it's because my dropdownlist value is null for parentGenre. I checked firebug and the correct value is being posted. Here is what I have so far:

Movies Model:

  public class Movies
{
    public Movies()
    {
        this.inventory = new HashSet<Inventory>();
        this.transactions = new HashSet<Transactions>();
    }

    [Key]
    public int movieID { get; set; }

    [Required(ErrorMessage = "Title is required")]
    [StringLength(50)]
    public String Title { get; set; }

    [Required(ErrorMessage = "Director is required")]
    [StringLength(30)]
    public String Director { get; set; }

    [Required(ErrorMessage = "An actor is required")]
    [StringLength(30)]
    public String Stars { get; set; }

    [Required(ErrorMessage = "Description is required")]
    [AllowHtml]
    [Column(TypeName = "nvarchar(MAX)")]
    public String Description { get; set; }

    [Required(ErrorMessage = "Genre is required")]
    public virtual Genres parentGenre { get; set; }

    [Required(ErrorMessage = "Duration is required")]
    public int Duration { get; set; }

    [Required(ErrorMessage = "Rating is required")]
    public String Rating { get; set; }

    [Required(ErrorMessage="Release date is required")]
    public DateTime releaseDate { get; set; }

    public ICollection<Inventory> inventory { get; set; }

    public ICollection<Transactions> transactions { get; set; }
}

Genres Model:

public class Genres
{
    public Genres()
    {
        this.movies = new HashSet<Movies>();
    }

    [Key]
    public int genreId { get; set; }

    [Required(ErrorMessage = "A genre name is required")]
    [StringLength(25)]
    public String genreName { get; set; }

    public ICollection<Movies> movies { get; set; }
}

Movies Controller:

public ActionResult addMovie(int? page)
    {

        ViewBag.Ratings = new SelectList(new[] { "G", "PG", "PG-13", "R", "NR" });



        ViewBag.parentGenre = new SelectList(movieRepository.Genres, "genreId", "genreName");
        return View();


    }

    #region "POST"
    [HttpPost]
    public ActionResult addMovie(Movies model)
    {
        if (ModelState.IsValid)
        {
            movieRepository.AddMovie(model);
            movieRepository.save(model);
            return RedirectToAction("index");
        }

        ViewBag.parentGenre = new SelectList(movieRepository.Genres, "genreId", "genreName");
        ViewBag.Ratings = new SelectList(new[] { "G", "PG", "PG-13", "R", "NR" });


        return View(model);

View:

@Html.DropDownList("parentGenre", String.Empty)

Just to summarize, it hits the if statement to check if the model is valid, and fails because the "parentGenres" is null, even though the value is being posted in firebug.

Edit: Entire View: @model MovieRental.Models.Movies

@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Movie was not entered in system. Please correct the errors and try again.")
    <div>
        <div class="input-prepend">
            <span class="add-on"><i class="icon-film"></i></span>
            @Html.TextBoxFor(m => m.Title, new { placeholder = "Title" })
            @Html.ValidationMessageFor(m => m.Title)
        </div>

        <div class="input-prepend">
            <span class="add-on"><i class="icon-facetime-video"></i></span>
            @Html.TextBoxFor(m => m.Director, new { placeholder = "Director" })
            @Html.ValidationMessageFor(m => m.Director)
        </div>

        <div class="input-prepend">
            <span class="add-on"><i class="icon-tags"></i></span>
            @Html.DropDownList("parentGenre", String.Empty)
            @Html.ValidationMessageFor(m => m.parentGenre)
        </div>  


        <div class="input-prepend">
            <span class="add-on"><i class="icon-star-empty"></i></span>
            @Html.TextBoxFor(m => m.Stars, new { placeholder = "Actor" })
            @Html.ValidationMessageFor(m => m.Stars)
        </div>



        <div class="input-prepend">
            <span class="add-on"><i class="icon-time"></i></span>
            @Html.TextBoxFor(m => m.Duration, new { @class="maskedDuration", placeholder = "Duration (mins)" })
            @Html.ValidationMessageFor(m => m.Duration)
        </div>

        <div class="input-prepend">
            <span class="add-on"><i class="icon-warning-sign"></i></span>
            @Html.DropDownListFor(m => m.Rating, (SelectList)ViewBag.Ratings)
            @Html.ValidationMessageFor(m => m.Rating)
        </div>



        <div class="input-prepend">
            <span class="add-on"><i class="icon-calendar"></i></span>
            @Html.TextBoxFor(m => m.releaseDate, new { @class="maskedDate", placeholder = "Release Date (mm/dd/yyyy)" })
            @Html.ValidationMessageFor(m => m.releaseDate)
        </div>


        <div class="control-group">
            <div class="input-prepend">
                <span class="add-on"><i class="icon-comment"></i></span>
                @Html.TextAreaFor(m => m.Description, new { placeholder = "Description", @class="input-xxlarge" })
                @Html.ValidationMessageFor(m => m.Description)
            </div>
        </div>

        <p><button class="btn btn-primary" type="submit" value="Submit">Submit</button></p>
        @Html.ValidationSummary()
    </div>
}

try changing this to a dropdownlistfor

@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenre, String.Empty)

except, I would strongly Recommend changing the name of your selectlist to parentGenreList as well

    ViewBag.parentGenreList = new SelectList(movieRepository.Genres, 
                               "genreId", "genreName");

so your resulting helper call would be

@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenreList , String.Empty)

The reason for this, is if the Select List and bound property share a name that often conflicts with inbound binding when the view renders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM