0
를 사용할 때이 같은 ActionLink
있습니다이상한 결과 모델 바인더
@Html.ActionLink("Click", "Create", "Default1", new {date = Model.Date} , null)
을 그리고 이것은 Create
입니다 :
public ActionResult Create(DateTime? date)
{
return View();
}
이보기 만들기에 완벽하게 같습니다
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Date) //I get the correct value here
<input type="submit" value="Save"/>
}
그러나 사용자 정의 모델 바인더를 사용하면 빈 결과가납니다. 왜? 내 모델 바인더는 간단하며 특별한 아무것도하지 않는 : 나는 global.asax
내가 TextBoxFor
에서 빈 문자열을 얻을 내에서이 모델 바인더를 등록한 후
public class SimpleModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return DateTime.Parse(valueResult.AttemptedValue);
}
}
합니다. 무슨 생각해?
ModelBinders.Binders.Add(typeof(DateTime?), new SimpleModelBinder());
그리고 내 모델 :
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueResult);
내가 필요 이유를 알고하지 않습니다
public class Test
{
public int Id { get; set; }
public DateTime? Date { get; set; }
}