Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:54:53+00:00 2026-05-14T04:54:53+00:00

A default value of 0 is creeping into a Surveys app I am developing,

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:54:54+00:00Added an answer on May 14, 2026 at 4:54 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I set default value of Who can see posts this app makes
You can have a default value by passing a dict to ConfigParser initialization. config
I have an input box that has default value text assigned to it. How
Can I set the default value for a function argument to be something that's
Is there a default value attribute that can be used in ASP.NET MVC to
I have set a default value in textarea. I want that when someone clicks
How can I set default value in ActiveRecord? I see a post from Pratik
I have a default value specified in XSD as follows: <xs:complexType name=image> <xs:attribute name=path
I have given a default value 2.5 for a textbox called diff_Box and then
how can i set default value in sonata admin bundle the data option is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.