我希望这有帮助:
模型类
public class TestModel
{
public string name { get; set; }
public TimeSpan TimeWorked { get; set; }
public TestModel()
{
TimeWorked = new TimeSpan(3, 20, 0);
name = "hello";
}
}
自定义模型绑定器
public class TimeModelBinder:DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = (TestModel)base.BindModel(controllerContext, bindingContext);
var Hour = controllerContext.HttpContext.Request["TimeWorked.Hours"];
var minutes = controllerContext.HttpContext.Request["TimeWorked.Minutes"];
var time = new TimeSpan(int.Parse(Hour), int.Parse(minutes), 0);
model.TimeWorked = time;
return model;
}
}
控制器
public ActionResult Index([ModelBinder(typeof(TimeModelBinder))]TestModel _model)
{
}