C# 自定义模型minder中的绑定错误会删除用户输入的值
我使用的是ASP.NET MVC 3 RTM,我有这样一个视图模型:C# 自定义模型minder中的绑定错误会删除用户输入的值,c#,asp.net-mvc-3,custom-model-binder,C#,Asp.net Mvc 3,Custom Model Binder,我使用的是ASP.NET MVC 3 RTM,我有这样一个视图模型: public class TaskModel { // Lot's of normal properties like int, string, datetime etc. public TimeOfDay TimeOfDay { get; set; } } public class TimeOfDayModelBinder : IModelBinder { public object BindModel(C
public class TaskModel
{
// Lot's of normal properties like int, string, datetime etc.
public TimeOfDay TimeOfDay { get; set; }
}
public class TimeOfDayModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
try
{
// Let the TimeOfDay struct take care of the conversion from string.
return new TimeOfDay(result.AttemptedValue, result.Culture);
}
catch (ArgumentException)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Value is invalid. Examples of valid values: 6:30, 16:00");
// This line is what makes the difference:
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, result);
return bindingContext.Model;
}
}
}
TimeOfDay
属性是我拥有的一个自定义结构,它非常简单,所以这里不包括它。我制作了一个自定义的模型绑定器来绑定这个结构。模型绑定器非常简单:
public class TimeOfDayModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
try
{
// Let the TimeOfDay struct take care of the conversion from string.
return new TimeOfDay(result.AttemptedValue, result.Culture);
}
catch (ArgumentException)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Value is invalid. Examples of valid values: 6:30, 16:00");
return bindingContext.Model; // Also tried: return null, return value.AttemptedValue
}
}
}
我的自定义模型绑定器工作正常,但问题是无法转换或解析用户提供的值。当发生这种情况时(当TimeOfDay构造函数抛出一个ArgumentException
),我添加了一个模型错误,该错误在视图中正确显示,但用户键入的无法转换的值丢失了。用户在其中键入值的文本框为空,在HTML源中,value属性设置为空字符串:“”
编辑:我想知道是否是我的编辑器模板做错了什么,所以我把它包括在这里:
@model Nullable<TimeOfDay>
@if (Model.HasValue)
{
@Html.TextBox(string.Empty, Model.Value.ToString());
}
else
{
@Html.TextBox(string.Empty);
}
@模型可为空
@if(Model.HasValue)
{
@TextBox(string.Empty,Model.Value.ToString());
}
其他的
{
@TextBox(string.Empty);
}
如何确保在发生绑定错误时不会丢失该值,以便用户可以更正该值?Aha!我终于找到了答案!他给出了答案。我缺少的是在我的模型绑定器中调用
ModelState.SetModelValue()
。所以代码是这样的:
public class TaskModel
{
// Lot's of normal properties like int, string, datetime etc.
public TimeOfDay TimeOfDay { get; set; }
}
public class TimeOfDayModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
try
{
// Let the TimeOfDay struct take care of the conversion from string.
return new TimeOfDay(result.AttemptedValue, result.Culture);
}
catch (ArgumentException)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Value is invalid. Examples of valid values: 6:30, 16:00");
// This line is what makes the difference:
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, result);
return bindingContext.Model;
}
}
}
我希望这能让别人从我经历的几个小时的挫折中解脱出来