MVC3下拉列表为空

本文关键字:下拉列表 MVC3 | 更新日期: 2023-09-27 18:06:06

我已经为此工作了好几天,但没有任何进展。我的Movies模型在我的帖子中一直失败,这是因为我的下拉列表中parentGenre的值为空。我检查了firebug,正确的值正在被张贴。以下是目前为止的内容:

电影模型:

  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; }
}

类型模型:

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; }
}

电影控制器:

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);

视图:

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

总结一下,它点击if语句来检查模型是否有效,并且失败了,因为"parenttypes"为空,即使该值在firebug中被发布。

编辑:整个视图:@ 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>
}

MVC3下拉列表为空

尝试将此更改为

下拉列表
@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenre, String.Empty)

除非,我强烈建议将选择列表的名称也更改为parentGenreList

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

所以你的帮助调用结果将是

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

这样做的原因是,如果选择列表和绑定属性共享一个名称,在视图呈现时经常与入站绑定冲突。