我已经在这方面工作了几天而且没有任何地方。我的电影模型在我的帖子中保持失败,这是因为我的dropdownlist值对于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语句来检查模型是否有效,并且因为“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>
}
答案 0 :(得分: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)
原因是,如果选择列表和绑定属性共享一个名称,该名称在视图呈现时通常与入站绑定冲突。