title: Object reference not set to an instance of an object on layout page
body: <p>I have an edit form in an edit page.When I come in the page for the first time everything is OK.But after submitting the form I see this error:</p>
  
  <blockquote>
    <p>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.</p>
    
    <p>Exception Details: System.NullReferenceException: Object reference not
    set to an instance of an object.</p>
  </blockquote>
  
  <p>And in the source error you see this:</p>
  
  <pre><code>@{
      ViewBag.Title = "Edit";
      Layout ="~/Views/Shared/_Layout.cshtml";
  }
  </code></pre>
  
  <p>What is wrong now?!</p>
  
  <p><strong>Update</strong></p>
  
  <p>My view codes:</p>
  
  <pre><code>@model Portal.Web.CMS.Models.PageViewModel
  
  @{
      ViewBag.Title = "ویرایش صفحه";
      Layout ="~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h3&gt;صفحه&lt;/h3&gt;
  &lt;ul class="breadcrumb" style="background-color: white;"&gt;
      &lt;li&gt;&lt;a href="~/Page"&gt;لیست صفحات&lt;/a&gt; &lt;span class="divider"&gt;/&lt;/span&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="@Url.Action("Details", "Page", new { url = Model.Url })"&gt;@Model.Name&lt;/a&gt; &lt;span class="divider"&gt;/&lt;/span&gt;&lt;/li&gt;
      &lt;li class="active"&gt;ویرایش صفحه&lt;/li&gt;
  &lt;/ul&gt;
  @using (Html.BeginForm())
  {
      @Html.ValidationSummary(true)
  
      @Html.HiddenFor(model =&gt; model.PageId)
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Lang)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;div class="btn-group" data-toggle="buttons-radio" dir="ltr"&gt;
              @if (Model.Lang.ToString() == "Fa")
              {
                  &lt;button type="button" value="2" class="btn"&gt;English&lt;/button&gt;
                  &lt;button type="button" value="1" class="btn active"&gt;فارسی&lt;/button&gt;
              }
              else
              {
                  &lt;button type="button" value="2" class="btn active"&gt;English&lt;/button&gt;
                  &lt;button type="button" value="1" class="btn"&gt;فارسی&lt;/button&gt;
              }
          &lt;/div&gt;
          @Html.Hidden("Lang")
      &lt;/div&gt;
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Name)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Name)
          @Html.ValidationMessageFor(model =&gt; model.Name)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Url)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Url)
          @Html.ValidationMessageFor(model =&gt; model.Url)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Title)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Title)
          @Html.ValidationMessageFor(model =&gt; model.Title)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.PageContent)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.TextAreaFor(model =&gt; model.PageContent)
          @Html.ValidationMessageFor(model =&gt; model.PageContent)
      &lt;/div&gt;
      &lt;div id="attachments" class="editor-field"&gt;
          &lt;p&gt;
              &lt;button type="button" id="addFile" class="btn btn success"&gt;فایل بیشتر&lt;/button&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;input type="file" name="files" /&gt;
          &lt;/p&gt;
      &lt;/div&gt;
      &lt;div id="productImages" class="editor-field"&gt;
          &lt;p&gt;
              &lt;button type="button" id="addPicture" class="btn btn success"&gt;عکس بیشتر&lt;/button&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;input type="file" name="images" /&gt;
          &lt;/p&gt;
      &lt;/div&gt;
      if (Model.PageAttachements!=null )
      {
      &lt;fieldset&gt;
          &lt;legend&gt;فایل های ضمیمه&lt;/legend&gt;
          &lt;div class="row-fluid show-grid"&gt;
              @foreach (var file in Model.PageAttachements)
              {
                  &lt;div class="span4 thumbnail"&gt;
                      &lt;a href="~/Page/DeleteFile/@file.PageAttachmentId" class="close pull-left" onclick="return confirm('آیا میخواهید مورد فوق را حذف نمایید؟')" &gt;&amp;times;&lt;/a&gt;
                      @file.Name
                  &lt;/div&gt;
              }
          &lt;/div&gt;
      &lt;/fieldset&gt;
      }
      if (Model.PageImages.Count &gt; 0)
      {
      &lt;fieldset&gt;
          &lt;legend&gt;تصاویر ضمیمه&lt;/legend&gt;
          &lt;div class="row-fluid show-grid"&gt;
              @foreach (var image in Model.PageImages)
              {
                  &lt;div class="span4 thumbnail"&gt;
                      &lt;a href="~/Product/DeleteImage/@image.PageImageId" class="close pull-left" onclick="return confirm('آیا میخواهید مورد فوق را حذف نمایید؟')" &gt;&amp;times;&lt;/a&gt;
                      &lt;img src="~/Images/@image.Name" /&gt;
                  &lt;/div&gt;
              }
          &lt;/div&gt;
      &lt;/fieldset&gt;
      }
      &lt;p&gt;
          &lt;input type="submit" value="ذخیره" class="btn btn-primary btn-large" /&gt;
      &lt;/p&gt;
  }
  
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p>And the controller:</p>
  
  <pre><code>public ActionResult Edit(int id)
  {
      var model = Mapper.Map&lt;PageViewModel&gt;(pageApp.GetPageById(id));
      return View(model);
  }
  [HttpPost]
  public ActionResult Edit(PageViewModel model)
  {
      bool result = false;
      if (model.Lang.ToString() == "Fa")
          result = pageApp.IsRepetitive(model.Url, 1);
      else
          result = pageApp.IsRepetitive(model.Url, 2);
      if (result == true)
      {
          TempData["error"] = "صفحه ای با همین زبان و آدرس وجود دارد.";
          return View();
      }
      else
      {
          if (model.PageContent != null)
          {
              var page = Mapper.Map&lt;Page&gt;(model);
              page.UserId = Portal.Web.CMS.Components.SessionContext.GetUserData().UserId;
              page.PageDate = DateTime.Now;
              pageApp.Update(page);
              TempData["success"] = "صفحه با موفقیت تغییر یافت.";
              return RedirectToAction("Details", new { url = model.Url });
          }
          else
          {
              TempData["error"] = "انجام نشد.صفحه خالی از مطلب است.";
              return View();
          }
      }
  }
  </code></pre>