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 7403431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:04:46+00:00 2026-05-29T05:04:46+00:00

I have been having an issue with updating a nullable bool value using TryUpdateModel.

  • 0

I have been having an issue with updating a nullable bool value using TryUpdateModel. I have a template created to handle the values as so:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Boolean?>" %>


<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
    <%= Html.DropDownListFor(model => model, new SelectListItem[] { new SelectListItem() { Text = "", Value = "null"},new SelectListItem() { Text = "Yes", Value = "true"}, new SelectListItem() { Text = "No", Value = "false"  }})%>
<% } else { %>
    <%= Html.CheckBoxFor(model => model.Value)%>
<% } %>

My View looks like this:

<%=Html.EditorFor(model => model.TestField) %>  //which looks/acts correctly

The SQL Server Database types are also defined correctly as a nullable bit.

My Code is straight forward:

  var so = new SomeObject();
  if (ModelState.IsValid)
  {
      //gets to here
      if (TryUpdateModel(so))
      {
          //never gets here
      }

   }

The Error reported for ModelState on that field is: “The value ‘null’ is not valid for TestField.”

This seems pretty straight forward, but I wasn’t able to find anything on this. Any help would be greatly appreciated.

Cheers,

Brian

  • 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-29T05:04:47+00:00Added an answer on May 29, 2026 at 5:04 am

    Since nobody has answered my question, I will put my workaround up. It’s not super elegant, but it works. If I wanted it to be pretty, it’d be in a pink font. 😉

    Basically I had to load “so” (someObject) manually using the form Collection like so…

    var so = new SomeObject();
    if (ModelState.IsValid)
    {
    
       so.WasItFound = StringToNullBool(form["WasItFound"]);
       so.WhereWasItFound = form["WhereWasItFound"];
    
       //fill in the rest of the properties using the form Collection...
    
    
    }
    
    
    
    private bool? StringToNullBool(string s)
    {
       if (s != "null")
          return Convert.ToBoolean(s);
       else
          return null;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been having an issue sine using the HABTM relationship in models instead of
I want to describe an issue I have been having with Plot using With
I have been having this issue in iterating through an array of keys and
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
We have been having this issue pop up sporadically, but now I can reproduce
I am somewhat new to JavaScript, and have been having an issue with a
I have been having issues with a custom check box control for a while
Hi, i have been having some problems using JSON data in flash builder lately
I have been having an issue with Visual Studio 2010 that is driving me
I have been having this issue for a while now and I cannot seem

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.