0

how do I render a textbox using textboxfor in MVC when I have a null class in the model.

For example I have the following I am using as my model

public class ClassOne
{
    public string classOneProperty {get;set;}
    public ClassTwo classTwoObject {get; set;}
}

public class ClassTwo
{
    public string classTwoProperty {get;set;}
}

So I have a table of class one values and the user clicks to edit an existing item of ClassOne. In ClassOne the object ClassTwo is null since it wasnt set at the initial creation of the item in the table, so when i try to do @Html.TextBoxFor(m => m.classTwoObject.classTwoProperty) I get a null reference error.

How can I use the TextBoxFor to edit fields that have null objects in them since I still want them to bind to the model on postback?

Thanks, DMan

1
  • Why not just set the model to a new ClassOne(){classTwoObject=new ClassTwo()}, or create a constructor in ClassOne which initializes classTwoObject ?
    – Brook
    Oct 22, 2012 at 17:50

2 Answers 2

2

When you return back your ClassOne you need to initialize the classTwoObject property to a new ClassTwo()

-Like Brook mentioned above. Beat me to it.

3
  • Simplest way of solving the issue. +1
    – Yan Brunet
    Oct 22, 2012 at 17:52
  • Indeed, it's so simple I thought I might be missing something so I asked as a comment first.
    – Brook
    Oct 22, 2012 at 17:55
  • @Lostdreamer Sorry I think I Should have clarified something a little here, Class one and two are stored in a DB and retrieved using EF, so in initializing the object before placing it into the view, isn't that going to make the EF SaveChanges on the postback think that there is an object there when there isn't actually and hence create null entry in the DB?
    – DMCApps
    Oct 22, 2012 at 18:03
1

You can create a constructor that initializes classTwoObject

public class ClassOne
{
    public string classOneProperty {get;set;}
    public ClassTwo classTwoObject {get; set;}
    public CLassOne()
    {
        classTwoObject = new ClassTwo();
    }

}

Or just initialize it inline

new ClassOne(){classTwoObject=new ClassTwo()};
7
  • Sorry I think I Should have clarified something a little here, Class one and two are stored in a DB and retrieved using EF, so in initializing the object before placing it into the view, isn't that going to make the EF SaveChanges on the postback think that there is an object there when there isn't actually and hence create null entry in the DB?
    – DMCApps
    Oct 22, 2012 at 18:06
  • yeah this is an obvious one I just thought this would make entries in the DB that are not accurate, but either way EF still makes an entry for it each time I don't understand why it wouldn't have an object for it then that just has null fields. Sorry for the dumb question should have looked at how EF and the DB were acting first.
    – DMCApps
    Oct 22, 2012 at 18:24
  • 1
    You original problem comes from using EF objects in views then.
    – Pluc
    Oct 22, 2012 at 19:01
  • 1
    Indeed, seriously consider using either DTOs or ViewModels for that purpose.
    – Brook
    Oct 22, 2012 at 19:14
  • @Brook I have thought about that but in the scale that I am using it would take more time to code that then it would to figure out an alternative as my Model would contain around 200 properties that I would have to map to the model then back to the EF Object. I would have definitely done it that way on a smaller scale unless I am missing an easier way to map EF to a DTO/ViewModel?
    – DMCApps
    Oct 22, 2012 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.