0

I have a viewmodel as:

public class AutoAdvert
{        
//
    public Condition condition { get; set; }
}

But when I access it in view it gives null reference error.

<div class="editor-label">
    @Html.LabelFor(model => model.condition.Tyres)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.condition.Tyres)
</div>

Where I am paasing to view:

public ActionResult autoadvert()
        {
            AutoAdvert autoadvert = new AutoAdvert();

            return View(autoadvert);
        }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64

2 Answers2

1

Your Condition is null. Add a Constructor to the autoadvert class and assign something to the variable.

public class AutoAdvert
{        
//
    public AutoAdvert(){
        Condition = New Condition();
    }
    public Condition condition { get; set; }
}

And you have to do the same thing in the Condition class to the Tyres if they are classes because the are null.

Daniel Björk
  • 2,475
  • 1
  • 19
  • 26
0

I suppose you have forgotten to initialize the class Condition while populating the model class AutoAdvert

Amit
  • 15,217
  • 8
  • 46
  • 68
Syed1510
  • 31
  • 1
  • 7