ASP.NET MVC:使用 HtmlHelper.TextBox 和自定义模型绑定器的空引用异常

发布于 2024-09-05 22:01:22 字数 1480 浏览 10 评论 0原文

我编写了一个实现 IModelBinder 的类(见下文)。此类处理一个具有 3 个输入的表单,每个输入代表日期值的一部分(日、月、年)。我还编写了相应的 HtmlHelper 扩展方法来打印表单上的三个字段。

当日、月、年输入给出可以解析的值,但单独的值验证失败时,一切都很好 - 字段被重新填充,页面按预期提供给用户。

但是,当提供无效值并且无法解析 DateTime 时,我会返回任意 DateTime ,以便在返回给用户时重新填充字段。

我阅读了人们遇到的类似问题,这些问题似乎都是由于缺少调用 SetModelValue() 造成的。我没有这样做,但即使添加后问题也没有解决。

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
   string modelName = bindingContext.ModelName;
   string monthKey = modelName + ".Month";
   string dayKey = modelName + ".Day";
   string yearKey = modelName + ".Year";

   //get values submitted on form
   string year = bindingContext.ValueProvider[yearKey].AttemptedValue;
   string month = bindingContext.ValueProvider[monthKey].AttemptedValue;
   string day = bindingContext.ValueProvider[dayKey].AttemptedValue;

   DateTime parsedDate;
   if (DateTime.TryParse(string.Format(DateFormat, year, month, day), out parsedDate))
        return parsedDate;

   //could not parse date time report error, return current date
   bindingContext.ModelState.AddModelError(yearKey, ValidationErrorMessages.DateInvalid);

   //added this after reading similar problems, does not fix!
   bindingContext.ModelState.SetModelValue(yearKey, bindingContext.ValueProvider[modelName]);
   return DateTime.Today;
}

当我尝试为日期的“年”属性创建文本框时,会引发空引用异常,但奇怪的是不是为“日”或“月”创建文本框!

谁能解释一下这是为什么?

I have written a class which implements IModelBinder (see below). This class handles a form which has 3 inputs each representing parts of a date value (day, month, year). I have also written a corresponding HtmlHelper extension method to print out three fields on the form.

When the day, month, year inputs are given values which can be parsed, but a separate value fails validation, all is fine - the fields are repopulated and the page served to the user as expected.

However, when an invalid values are supplied and a DateTime cannot be parsed, i return an arbitrary DateTime so that the fields will be repopulated when returned to the user.

I read up on similar problems people have had and they all seemed to be due to lack of calling SetModelValue(). I wasn't doing this, but even after adding the problem has not been resolved.

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
   string modelName = bindingContext.ModelName;
   string monthKey = modelName + ".Month";
   string dayKey = modelName + ".Day";
   string yearKey = modelName + ".Year";

   //get values submitted on form
   string year = bindingContext.ValueProvider[yearKey].AttemptedValue;
   string month = bindingContext.ValueProvider[monthKey].AttemptedValue;
   string day = bindingContext.ValueProvider[dayKey].AttemptedValue;

   DateTime parsedDate;
   if (DateTime.TryParse(string.Format(DateFormat, year, month, day), out parsedDate))
        return parsedDate;

   //could not parse date time report error, return current date
   bindingContext.ModelState.AddModelError(yearKey, ValidationErrorMessages.DateInvalid);

   //added this after reading similar problems, does not fix!
   bindingContext.ModelState.SetModelValue(yearKey, bindingContext.ValueProvider[modelName]);
   return DateTime.Today;
}

The null reference exception is thrown when i attempt to create a textbox for the Year property of the date, but strangely not for Day or Month!

Can anyone offer an explanation as to why this is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-09-12 22:01:22

这应该可以解决这个问题:

bindingContext.ModelState.AddModelError(
    yearKey, 
    ValidationErrorMessages.DateInvalid
);

bindingContext.ModelState.SetModelValue(
    yearKey, 
    bindingContext.ValueProvider[modelName]
);

请注意,必须使用相同的密钥 (yearKey)。

This should fix it:

bindingContext.ModelState.AddModelError(
    yearKey, 
    ValidationErrorMessages.DateInvalid
);

bindingContext.ModelState.SetModelValue(
    yearKey, 
    bindingContext.ValueProvider[modelName]
);

Notice that the same key must be used (yearKey).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文