這是什麼問題?下拉列表始終設置模型狀態無效
@Html.DropDownListFor(x => x.Post.CategoryId, new SelectList(Model.Categories, "ID", "CategoryName"), "-- Please select --")
@Html.ValidationMessageFor(x => x.Post.CategoryId)
當我提交表單的ModelState是無效的,這個下拉列表中被標記爲錯誤,即使我從下拉列表中的值。
(順便說一句類別ID是在我的崗位表的FK) 我haved加入這個「哥們」級爲部分
[MetadataType(typeof(Post_Validation))]
public partial class Post
{
}
class Post_Validation
{
[Required(ErrorMessage = "Please select a Category")]
public int CategoryId { get; set; }
}
控制器:
// GET: /News/Add
[Authorize]
public ActionResult Add()
{
var model = new AddPostViewModel() { Categories = categoryRepository.GetAllCategories(), Post = new Post(), IsEditMode=false };
return View(model);
}
//
// POST: /News/Add
[Authorize]
[HttpPost]
public ActionResult Add(AddPostViewModel model)
{
if (!ModelState.IsValid)
{
..etc
}
}
該模型顯示正常,當我刪除DDL類別的validationfor它提交沒問題。 CategoryId是一個int ...可能是一個問題? – raklos 2011-01-22 17:40:52