ASP.NET MVC Html.RadioButton异常 [英] ASP.NET MVC Html.RadioButton Exception

查看:111
本文介绍了ASP.NET MVC Html.RadioButton异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我哈弗我的网页上一个简单的单选按钮列表,我和我的看法如下渲染:

 <标签=性别>性别与LT; /标签>
<%= Html.RadioButton(性别,1)%GT;男
<%= Html.RadioButton(性别,2)%GT;女
<%= Html.ValidationMessage(性别)%GT;

请注意,当用户最初看到这个输入,既不按钮被选择。
验证有没有迫使他们选择和不接受违约。
因此,这两个单选按钮绑定到声明为在我的模型可为空的int属性:

 公众诠释?性别{搞定;组; }

所以,如果他们不选择一个按钮,并提交页面,性别属性将为null表示他们没有选择。以下验证控制器的帖子中所谓的:

 如果(!gender.HasValue)
    ModelState.AddModelError(性别,性别要求);

但是,如果验证失败(他们没有选择),然后在呈现阶段,以下异常是由MVC框架抛出:

  System.NullReferenceException是由用户code未处理
  消息=对象引用不设置到对象的实例。

在寻找一个解决这个问题,我注意到,有几个有这个问题。
我使用ASP.NET MVC 1.0。我发现在这里这个错误是使用.NET反射甩在了code的地方。

现在的问题是如何正确地做这项工作?

编辑:添加堆栈跟踪:

  System.NullReferenceException是由用户code未处理
  消息=对象引用不设置到对象的实例。
  来源=System.Web.Mvc
  堆栈跟踪:
       在System.Web.Mvc.HtmlHelper.GetModelStateValue(字符串键,类型destinationType)
       在System.Web.Mvc.Html.InputExtensions.InputHelper(的HtmlHelper的HtmlHelper,inputType下inputType下,字符串名称,对象的值,布尔useViewData,布尔器isChecked,布尔SETID,布尔isExplicitValue,IDictionary`2 htmlAttributes)
       在System.Web.Mvc.Html.InputExtensions.RadioButton(的HtmlHelper的HtmlHelper,字符串名称,对象的值,布尔器isChecked,IDictionary`2 htmlAttributes)
       在System.Web.Mvc.Html.InputExtensions.RadioButton(的HtmlHelper的HtmlHelper,字符串名称,对象的值,IDictionary`2 htmlAttributes)
       在System.Web.Mvc.Html.InputExtensions.RadioButton(的HtmlHelper的HtmlHelper,字符串名称,对象的值)
       在ASP.views_vbs_register_aspx .__ RenderregisterContent(HtmlTextWriter的__w,控制parameterContainer)在c:\\Users\\David\\Documents\\BellevueProject\\Bellevue\\BellevueTeachers\\Forms\\Views\\VBS\\Register.aspx:line 42


解决方案

我只是想东西,使这项工作。
如果我不做验证步骤不会发生的问题,但我当然需要验证。
这给了我为解决方案的线索。

该ValidationMessage的HtmlHelper方法接受一个字符串参数是要验证的属性或模型对象的名称。
我只是改变了名称为gender2如下:

 <标签=性别>性别与LT; /标签>
<%= Html.RadioButton(性别,1)%GT;男
<%= Html.RadioButton(性别,2)%GT;女
&所述;%= Html.ValidationMessage(gender2)%>

和我换了验证code引用这个新的名称(即使该属性不存在,它仍然有效):

 如果(!gender.HasValue)
    ModelState.AddModelError(gender2,性别要求);

这工作应有的作用。

我本来以为对方应该有工作,但是这是一个简单的解决方法,我记录在这里。

编辑:
顺便说一句我试图改变性别属性设置为一个字符串,而不是一个可空INT,并出现完全相同的问题。

变通方法似乎仍然在使用不同的密钥名称验证消息。

I haver a simple radio button list on my page that I render with the following in my view:

<label for="gender">Gender</label>
<%= Html.RadioButton("gender", 1) %> Male
<%= Html.RadioButton("gender", 2) %> Female
<%= Html.ValidationMessage("gender") %>

Note that when the user initially sees this input, neither button is selected. The validation is there to force them to choose and not accept a default. Therefore, these two radio buttons are bound to a nullable int property in my model declared as:

public int? gender { get; set; }

So if they do not select a button, and submit the page, the gender property will be null indicating that they did not select. The following validation is called by the controller during the post:

if (!gender.HasValue)
    ModelState.AddModelError("gender", "gender required");

But, if the validation fails (they did not choose), then during the rendering phase, the following exception is thrown by the MVC framework:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."

In searching for for a solution to this problem, I noted several had this problem. I am using ASP.NET MVC 1.0. I found the place in the code where this error is thrown using .NET Reflector.

The question is how to make this work correctly?

EDIT: to add stacktrace:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="System.Web.Mvc"
  StackTrace:
       at System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType)
       at System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes)
       at System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value, Boolean isChecked, IDictionary`2 htmlAttributes)
       at System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value, IDictionary`2 htmlAttributes)
       at System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value)
       at ASP.views_vbs_register_aspx.__RenderregisterContent(HtmlTextWriter __w, Control parameterContainer) in c:\Users\David\Documents\BellevueProject\Bellevue\BellevueTeachers\Forms\Views\VBS\Register.aspx:line 42

解决方案

I just tried something that makes this work. The problem does not occur if I do not do the validation step but of course I need the validation. That gave me a clue for the solution.

The ValidationMessage HtmlHelper method takes a string argument that is the name of the property or model object being validated. I just changed that name to be "gender2" as follows:

<label for="gender">Gender</label>
<%= Html.RadioButton("gender", 1) %> Male
<%= Html.RadioButton("gender", 2) %> Female
<%= Html.ValidationMessage("gender2") %>

And I changed the validation code to refer to this new name (even though that property does not exist, it still works):

if (!gender.HasValue)
    ModelState.AddModelError("gender2", "gender required");

This works as desired.

I would have thought the other should have worked, but this is a simple workaround and I am documenting that here.

EDIT: By the way I tried changing the gender property to a string instead of a nullable int, and the same exact problem occurs.

The work around still seems to be in using a different key name for the Validation Message.

这篇关于ASP.NET MVC Html.RadioButton异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆