モデルバインディングは簡単です。それを恐れないでください。厳密に型指定されたビューは、将来の可読性と保守性のためにコードをクリーンに保ちます。マイクロソフトは、私たちのような人々が使用できるように、これらの新機能をリリースします。私たちはそれらを恐れるべきではありません!
DateTime 値を保持するプロパティを用意し、他のライブラリを使用して、エンド ユーザーに優れた日付選択エクスペリエンスを提供してみませんか? jQuery UI カレンダーは、考えられる 1 つのオプションです。
http://jqueryui.com/demos/datepicker/
ViewModel に DateTime タイプ プロパティを追加します。
public class AlbumViewModel
{
public int ID { set;get;}
public DateTime CreatedDate { set;get;}
}
get アクション メソッドで、このビューモデルのオブジェクトをビューに返すことができます。
public ActionResult AddAlbum()
{
AlbumViewModel objItem=new AlbumViewModel();
return View(objItem);
}
このビューモデルで厳密に型指定されたビューを使用し、jQuery UI カレンダーを使用してカレンダーを取得します。
@model AlbumViewModel
//include jQuery and jQuery UI
@using (Html.BeginForm())
{
<p>Select Date
@Html.TextBoxFor(m=>m.CreatedDate)
<input type="submit" value="Save" />
</p>
}
<script type="text/javascript">
$(function() {
$( "#CreatedDate").datepicker();
});
</script>
そしてあなたの投稿で アクション
[HttpPost]
public ActionResult AddAlbum(AlbumViewModel objItem)
{
//Check the objItem.CreatedDate here. Save it or do whatever you want
return View(objItem);
}