Html.CheckBoxFor TypeConversition错误

时间:2015-12-04 15:42:35

标签: c# asp.net-mvc razor html-helper

我有一个视图模型:

public class RegisterModel
{
   ...
   public bool Confirmation{ get; set; }
}

我在我的视图中使用复选框助手:

@model RegisterModel

......
@Html.CheckBoxFor(m => m.Confirmation) 

此复选框html助手创建:

<input id="Confirmation" name="Confirmation" value="true" type="checkbox">
<input name="Confirmation" value="false" type="hidden">

在控制器上

[HttpPost]
[ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
{
   if (!ModelState.IsValid)
                return View(model);
.....
}

让我们说一些用户将输入值更改为&#39; xxx&#39;发布它。因此,Model无效,我们返回视图。之后,Html.CheckBoxFor会出现此错误:

  

参数转换类型&#39; System.String&#39;输入   &#39; System.Boolean&#39;失败。

内部例外:

  

System.FormatException:xxx不是布尔值

的有效值

当我们返回视图时:Model.Confirmation false Request["Confirmation"]值为&#39; xxx&#39;

此错误来自ValueProviderResult方法上的ConvertSimpleType类。我认为,它试图将Request["Confirmation"]值转换为布尔值,它会给出错误。

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Conversion failure is not fatal")]
private static object ConvertSimpleType(CultureInfo culture, object value, Type destinationType)
{

.....
    TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
    bool canConvertFrom = converter.CanConvertFrom(value.GetType());
    if (!canConvertFrom)
    {
        converter = TypeDescriptor.GetConverter(value.GetType());
    }
    if (!(canConvertFrom || converter.CanConvertTo(destinationType)))
    {
        // EnumConverter cannot convert integer, so we verify manually
        if (destinationType.IsEnum && value is int)
        {
            return Enum.ToObject(destinationType, (int)value);
        }
             string message = String.Format(CultureInfo.CurrentCulture, MvcResources.ValueProviderResult_NoConverterExists,
                                           value.GetType().FullName, destinationType.FullName);
        throw new InvalidOperationException(message);
    }

.....
}

如何修复或避免此错误?

1 个答案:

答案 0 :(得分:3)

根据@StephenMuecke,这是默认行为。您可以查看详细的answer

根据@ataravati,我们应该在model.IsValid==false处理这个问题。如果model无效,我们删除checkbox的值并指定新值。因此,当我们返回视图时,我们不会收到任何错误。

 if (!ModelState.IsValid)
            {
                bool confirmation;

                bool.TryParse(Request["Confirmation"],out confirmation);
                ModelState.Remove("Confirmation");
                request.Confirmation = confirmation;
                return View(request);
            }

根据@StephenMuecke,如果复选框输入值不是布尔值,则用户肯定是恶意的。因此,我们将用户重定向到另一个具有跟踪/阻止ip算法的操作,并将404作为视图返回。

  if (!ModelState.IsValid)
            {
                bool confirmation;
                if (bool.TryParse(Request["Confirmation"], out confirmation))
                    return View(request);
                return RedirectToAction("Http404", "Errors"); //This not just redirecting 404, it has also tracking/blocking ip algorithm.
            }
相关问题