Html.RadioButton 组默认为 0
默认值 0 正在潜入我正在开发的调查应用程序中,但我看不到任何原因。问题如下:
我有一组 Html.RadioButtons,它们代表用户可以选择回答调查问题的可能值(1 == 一点也不,2 == 一点,3 == 很多)。我使用了tinyint 数据类型(不允许为空)来存储每个问题的答案。
查看代码如下所示:
<ol class="SurveyQuestions">
<% foreach (SurveyQuestion question in Model.Questions)
{
string col = question.QuestionColumn;
%>
<li><%=question.QuestionText%>
<ul style="float:right;" class="MultiChoice">
<li><%= Html.RadioButton(col, "1")%></li>
<li><%= Html.RadioButton(col, "2")%></li>
<li><%= Html.RadioButton(col, "3")%></li>
</ul>
<%= Html.ValidationMessage(col, "*") %>
</li>
<% } %>
</ol>
[上述代码的注释:]
每个调查大约有 70 个问题,因此我将问题文本放在一个表中,并将结果存储在另一个表中。我已将问题放入表单视图模型中(因此称为 Model.Questions);问题表有一个名为 QuestionColumn 的字段,它允许我将答案表列链接到问题,如上所示(<%= Html.RadioButton(col, "1")%> 等)
[/Note]
但是,当用户不回答问题时,值 0 就会被插入到数据库列中。如果用户没有选择任何单选按钮,我期望为 Null。
结果,我没有得到我所期望的结果,即由于空值(没有用户输入)而导致的验证错误。
我没有在任何地方为答案表中的字段规定默认值 0。
那么到底发生了什么?有什么想法吗?
A default value of 0 is creeping into a Surveys app I am developing, for no reason that I can see. The problem is as follows:
I have a group of Html.RadioButtons that represent the possible values a user can choose to answer a survey question (1 == Not at all, 2 == A little, 3 == A lot). I have used a tinyint datatype, that does not allow null, to store the answer to each question.
The view code looks like this:
<ol class="SurveyQuestions">
<% foreach (SurveyQuestion question in Model.Questions)
{
string col = question.QuestionColumn;
%>
<li><%=question.QuestionText%>
<ul style="float:right;" class="MultiChoice">
<li><%= Html.RadioButton(col, "1")%></li>
<li><%= Html.RadioButton(col, "2")%></li>
<li><%= Html.RadioButton(col, "3")%></li>
</ul>
<%= Html.ValidationMessage(col, "*") %>
</li>
<% } %>
</ol>
[Note on the above code:]
Each survey has about 70 questions, so I have put the questions text in one table, and store the results in a different table. I have put the Questions into my form view model (hence Model.Questions); the questions table has a field called QuestionColumn that allows me to link up the answer table column to the question, as shown above (<%= Html.RadioButton(col, "1")%>, etc)
[/Note]
However, when the user DOESN'T answer the question, the value 0 is getting inserted into the database column. I would expect Null if the user doesn't select any of the radiobuttons.
As a result, I don't get what I expect, ie, a validation error due to the null value (no user input).
In no place have I stipulated a default value of 0 for the fields in the answers table.
So what is happening? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我已经破解了它:
由于 SurveyAnswers 表具有不可为空字段的列表(表示用户输入),因此 dbml 文件中的 Linq to Sql 生成的类具有字节类型的字段。
因此,当 MVC 创建一个新的 SurveyAnswer 对象来添加 Forms 值时,字节类型字段的默认值为 0。如果该值在 Form 中未更改,则它将保持为 0,这就是发送的内容到数据库。我期待的是空。尼古拉斯也可能是对的:当没有选择任何单选按钮时,浏览器返回值 0。生活和学习。我会检查尼古拉斯的帖子作为答案。
为了解决这个问题,我正在执行以下操作:
为 SurveyAnswers 中的每个字段(其中有超过 70 个......)编写检查约束,将输入限制为 (namefield > 0 AND nameField < 4)。这样我就能保证只有有效的数据才会进入调查。
由于这些检查约束,在控制器中启动的 POST 操作的数据库更新将失败,并且在 catch 块中,我可以向 ModelState 添加验证消息并返回到输入视图。
I think I have cracked it:
As the SurveyAnswers table has a list of non nullable fields (representing user input), the class generated by Linq to Sql in the dbml file has fields with byte type.
As a result, when MVC creates a new SurveyAnswer object to add the Forms values to, the default value of the byte type fields is 0. If this value is not changed in the Form, then it stays as 0 and that is what is sent to the database. I was expecting null. Nicholas may also be right: the browser returns a value of 0 when none of the radio buttons is selected. Live and learn. I will check Nicholas' post as the answer.
To solve this, I am doing the following:
Writing a check constraint for each field in SurveyAnswers (there are over 70 of them...) that restricts input to (namefield > 0 AND nameField < 4). That way I guarantee that only valid data goes into the surveys.
As a result of these check constraints, the database update initiated in the Controller for the POST action will fail, and in the catch block, I can add a validation message to the ModelState and return to the input View.
由于您尚未将任何按钮设置为默认检查,因此浏览器可能会提供自己的值 - 在本例中为 0。
根据 w3.org 作者应确保每组单选按钮中有一个最初处于“打开”状态
As you have not set any of your buttons as default checked the browser may be supplying its own value - in this case 0.
According to w3.org authors should ensure that in each set of radio buttons that one is initially "on"