Html.RadioButton group defaulting to 0
Posted
by awrigley
on Stack Overflow
See other posts from Stack Overflow
or by awrigley
Published on 2010-03-31T14:10:54Z
Indexed on
2010/03/31
14:13 UTC
Read the original article
Hit count: 233
Hi
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. As a result, I don't get what I expect, ie, a validation error.
In no place have I stipulated a default value of 0 for the fields in the answers table.
So what is happening? Any ideas?
© Stack Overflow or respective owner