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"/>
}
私のカスタムモデルバインダーを使用すると空の結果が得られます。どうして?私のモデルバインダーは単純であり、それは特別な何もしません。
public class SimpleModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return DateTime.Parse(valueResult.AttemptedValue);
}
}
私はglobal.asax
にこのモデルバインダーを登録した後、私はTextBoxFor
に空の文字列を取得します。何かお考えですか?
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; }
}