1
我已經創建了自己的自定義模型綁定來處理我的觀點限定的區間的DropDownList:自定義模型綁定的DropDownList的選擇不正確的價值
Html.DropDownListFor(m => m.Category.Section, new SelectList(Model.Sections, "SectionID", "SectionName"), "-- Please Select --")
這裏是我的模型綁定:
public class SectionModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (bindingContext.ModelType.IsAssignableFrom(typeof(Section)) && value != null)
{
if (Utilities.IsInteger(value.AttemptedValue))
return Section.GetById(Convert.ToInt32(value.AttemptedValue));
else if (value.AttemptedValue == "")
return null;
}
return base.BindModel(controllerContext, bindingContext);
}
}
現在我的控制之內,我可以說:
[HttpPost]
public ActionResult Create(FormCollection collection)
{
var category = new Category();
if (!TryUpdateModel(category, "Category")
return View(new CategoryForm(category, _sectionRepository().GetAll()));
...
}
這很好地驗證併爲secti正確的值on在模型更新時分配,但如果另一個屬性不驗證,它不會選擇正確的值。
我會很感激,如果有人能告訴我如何做到這一點。說解決了由於