0

私はこれに数日間取り組んでいますが、どこにも行きません。投稿で映画モデルが失敗し続けます。これは、parentGenre のドロップダウン リストの値が null であるためです。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)

要約すると、値がfirebugに投稿されているにもかかわらず、モデルが有効かどうかを確認するためにifステートメントにヒットし、「parentGenres」がnullであるため失敗します。

編集: ビュー全体: @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>
}

4

1 に答える 1

1

これをドロップダウンリストに変更してみてください

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

これは、選択リストとバインドされたプロパティが共有する名前が、ビューのレンダリング時にインバウンド バインディングと競合することが多い場合に発生します。

于 2013-03-28T00:01:20.013 に答える