我已经为此工作了几天,但一无所获。我的电影模型在我的帖子中一直失败,这是因为我的下拉列表值对于 parentGenre 为空。我检查了萤火虫并发布了正确的值。这是我到目前为止所拥有的:
电影型号:
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 语句来检查模型是否有效,并且由于“parentGenres”为空而失败,即使该值已发布在 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>
}