我有一个来自剃刀的Time(例如:5:00 PM)字符串类型,我想在将其传递给控制器时将其绑定到timespan
。
示例代码:
剃刀:
@model Project.Models.MyModel
@Html.TextBoxFor(m => m.Time, new { @class = "sTime" , @id = "txtTime" })
<script type="text/javascript">
$(function () {
$(".sTime").kendoTimePicker({
format: "h:mm tt",
parseFormats: ["HH:mm"]
});
$("#txtTime").data("kendoTimePicker").value("@Model.Time");
});
</script>
控制器:
[HttpPost]
public ActionResult Edit(MyModel model)
{
//some code here.
return View();
}
型号:
public class MyModel
{
public TimeSpan Time { get; set; }
}
现在,问题是该时间无法从剃刀绑定到控制器。帖子值始终为{00:00:00}。
如何使用自定义Model Binder实现这一目标?
答案 0 :(得分:1)
您可以使用textboxfor()
或hiddenfor()
将值传递到您的controller
@{
var modelList = (List<MyModel>)ViewBag.List;
}
@foreach (var item in modelList) {
@Html.TextBox("time", item.Time.HasValue ? item.Time.Value.ToString() :
"", new { @class = "sTime" })
@Html.HiddenFor(m=>m.Time) //just add this line with your model value.
}
<script type="text/javascript">
$(function () {
$(".sTime").kendoTimePicker({
format: "h:mm tt",
parseFormats: ["HH:mm"]
});
});
</script>