Q:

HTML.Editor with null values

I have the folloiwng _CreateOrEdit partial view:-
@model TMS.ViewModels.RackJoin
@* This partial view defines form fields that will appear when creating and editing entities *@
 @Html.AntiForgeryToken()
<div >
    <span class="f">RU </span>
    @Html.EditorFor(model => model.Rack.RU)
    @Html.ValidationMessageFor(model => model.Rack.RU)
</div>

<div >
    <span class="f">Rack Name </span>
    @Html.Editor("assetName", (Model.Resource.RESOURCENAME == null ? String.Empty : Model.Resource.RESOURCENAME))

    @Html.ValidationMessageFor(model => model.Resource.RESOURCENAME)
</div>

<div >

the view will work well when editing an exsiting record, but if it is a new object i will get the following exception:-

Object reference not set to an instance of an object.   Description:
  An unhandled exception occurred during the execution of the current
  web request. Please review the stack trace for more information about
  the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference
  not set to an instance of an object.

on the following code inside the view:-
@Html.Editor("assetName", (Model.Resource.RESOURCENAME == null ? String.Empty : Model.Resource.RESOURCENAME))

UPDATE
The ACTION METHOD THAT CALLS THE ABOVE VIEW IS :-
  public ActionResult Create()
        {
            ViewBag.PossibleDataCenters = repository.AllDataCenter().OrderBy(a => a.Name).ToList() ;
            ViewBag.PossibleZones = repository.AllZone().OrderBy(a => a.Name).ToList();
            ViewBag.Products = repository.GetProducts("Rack").OrderBy(a => a.COMPONENTNAME).ToList();

            return View();
        } 

A:

i think the problem is that when you are trying to pass in a new object then either you are passing in null model to the View or all the propertiese of the model are not initialized
so in the line
Model.Resource.RESOURCENAME

if either Model is null OR Model.Resource is null it will give the same error you are getting.
Try checking for NULL before using your Model

