MVCToolkit and nulls

Last post 05-16-2009 2:51 PM by Augi. 3 replies.

Sort Posts:

  • MVCToolkit and nulls

    12-19-2007, 8:44 AM
    • Contributor
      4,372 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

    Hi

    I'd like the MVCToolkit to be tolerant of nulls. I've been using ?? "" to avoid the null errors.

    <%= Html.TextBox( "Name", ViewData.Name ?? "" ) %> 

     

    As an aside, I'm really excited about extension methods, cos you can call them on null objects!!!!!!

    public static string MyToString(this object obj)
    {
        return obj == null ? String.Empty : obj.ToString();
    }

    // then you can do this;

    object a = null;

    a.MyToString(); // This looks like you'll get a NullReferenceException

    But you don't! No errors, It returns the empty string if a is null. Something like this should be baked into the framework if you ask me.
     

  • Re: MVCToolkit and nulls

    12-19-2007, 1:34 PM
    • Member
      574 point Member
    • abombss
    • Member since 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

     +1, NRE's in views are a pain.

    Adam Tybor -- abombss.com
    Filed under: , ,
  • Re: MVCToolkit and nulls

    05-16-2009, 1:27 PM
    • Member
      4 point Member
    • ekorahmat
    • Member since 05-16-2009, 5:12 PM
    • Posts 2

     i'm not checking the null value, i just use

     <%= Html.TextBox( "Name" ) %>

    and it works for me

  • Re: MVCToolkit and nulls

    05-16-2009, 2:51 PM
    • Contributor
      6,355 point Contributor
    • Augi
    • Member since 10-18-2008, 5:46 AM
    • Czech Republic
    • Posts 1,101
    • TrustedFriends-MVPs
    Don't forget to click "Mark as Answer" on the post that helped you.
Page 1 of 1 (4 items)