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 ? Commented 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Simplest way of solving the issue. +1
Indeed, it's so simple I thought I might be missing something so I asked as a comment first.
@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?
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 Comments

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?
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.
You original problem comes from using EF objects in views then.
Indeed, seriously consider using either DTOs or ViewModels for that purpose.
@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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.