カテゴリ付きの投稿を追加したいのですが、私のAdd
アクションBlogController
は次のようになります:
private readonly IBlogPostRepository _blogPostRepository;
private readonly ICategoryRepository _categoryRepository;
public BlogController()
{
_blogPostRepository = new BlogPostRepository(new SiteContext());
_categoryRepository = new CategoryRepository(new SiteContext());
}
public BlogController(IBlogPostRepository blogPostRepository, ICategoryRepository categoryRepository)
{
_blogPostRepository = blogPostRepository;
_categoryRepository = categoryRepository;
}
public ActionResult Add()
{
ViewData["categoryList"] = _categoryRepository.GetAllCategory();
return View("Add");
}
[HttpPost]
public ActionResult Add(BlogPost blogPost)
{
if (ModelState.IsValid)
{
blogPost.PublishDate = DateTime.Now;
_blogPostRepository.AddPost(blogPost);
_blogPostRepository.Save();
return RedirectToAction("Add");
}
return new HttpNotFoundResult("An Error Accoured while requesting your order!");
}
私の最初の質問は、なぜカテゴリリストをSelectList
かみそりでキャストしてカテゴリを選択できないのDropDownList
ですか?ビューにある私のコードは次のようになります。
<div>
@Html.LabelFor(b => b.Category)
@Html.DropDownList("Category", ViewData["categoryList"] as SelectList)
@Html.ValidationMessageFor(b => b.Category)
</div>
私の2番目の質問は、リクエストに応じてカテゴリを追加するにはどうすればよいAdd
ですPOST
か?