Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:25:18+00:00 2026-05-25T21:25:18+00:00

I have a complex object that I want to use in an edit view.

  • 0

I have a complex object that I want to use in an edit view. To simplify things I have created a ViewModel and have successfully created the edit view page, and everything renders correctly. When I hit save, everything falls apart.

The ViewModel is as follows:

public class ClosureEditViewModel
{

    public Model.Closure Closure { get; set; }
    public Model.School School { get; set; }
    public Model.ClosureDetail CurrentDetails { get; set; }
}

Some of the View is as follows:

<div class="display-label">School</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Closure.School.Name)
</div>
<div class="display-label">Closed</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Closure.Logged)
</div>
....
<div class="editor-label">
    @Html.LabelFor(model => model.CurrentDetails.DateOpening, "Date Opening (dd/mm/yyyy)")
</div>
<div class="editor-field">
    @Html.TextBox("DateOpening", Model.CurrentDetails.DateOpening.ToString("dd/MM/yyyy"))
    @Html.ValidationMessageFor(model => model.CurrentDetails.DateOpening)
</div>
....
    <tr>
        <td>
            @Html.CheckBoxFor(model => model.CurrentDetails.Nursery, (Model.School.Nursery ? null : new { @disabled = "disabled" }))
        </td>

The important parts of the controller are as follows:

    public ActionResult Edit(int id)
    {
        Data.IClosureReasonRepository reasonRepository = new Data.SqlServer.Repositories.ClosureReasonRepository(UnitOfWork);
        IEnumerable<Model.ClosureReason> reasons = reasonRepository.GetAll();

        Model.Closure closure = ClosureRepository.GetClosure(id);
        Model.ClosureDetail currentDetail = closure.ClosureDetails.Last();
        ViewModels.ClosureEditViewModel editClosure = new ViewModels.ClosureEditViewModel() { Closure = closure, School = closure.School, CurrentDetails = closure.ClosureDetails.Last() };
        ViewBag.ReasonId = new SelectList(reasons, "Id", "Name", currentDetail.ReasonId);
        return View(editClosure);
    }

    [HttpPost]
    public ActionResult Edit(ViewModels.ClosureEditViewModel newDetail)
    {
        //if (ModelState.IsValid)
        //{

        //}

        Data.IClosureReasonRepository reasonRepository = new Data.SqlServer.Repositories.ClosureReasonRepository(UnitOfWork);
        IEnumerable<Model.ClosureReason> reasons = reasonRepository.GetAll();
        ViewBag.ReasonId = new SelectList(reasons, "Id", "Name", newDetail.CurrentDetails.ReasonId);
        return View(newDetail);
    }

When I hit save the following message appears:

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.

Source Error: 


Line 94:                 </td>
Line 95:                 <td>
Line 96:                     @Html.CheckBoxFor(model => model.CurrentDetails.P1, (Model.School.P1 ? null : new { @disabled = "disabled" }))
Line 97:                 </td>
Line 98:                 <td>

I just can’t figure out why it is having problems with the School property but neither of the other two.

James 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:25:19+00:00Added an answer on May 25, 2026 at 9:25 pm

    It seems that Model.School is null when you render the view once again in the POST action. Make sure that it isn’t null because in your view you don’t have a single input field bound to the School property => this property will be null inside your POST controller action.

    [HttpPost]
    public ActionResult Edit(ClosureEditViewModel viewModel)
    {
        ... some operations
    
        // Make sure that viewModel.School is not null
        // Remember that the checkbox is bound to CurrentDetails.P1 so 
        // when you post to this action there is nothing that will initialize
        // the School property => you should do whatever you did in your GET
        // action in order to initialize this property before returning the view
        return View(viewModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use swig for generation of read-only wrappers for a complex object.
I have a complex object hierarchy that I've been wrestling with to get passed
If i have a complex object, what is the best practice pattern to write
I have a pretty complex object graph G with an object o1 in G
What is the format for databinding to a complex object? I have a linq
I have a List of a complex type - an object with a few
I have complex GUI application written in Python and wxPython. I want it to
I have a complex .NET Remoting server app that provides a couple of services.
I have a complex WPF control that for some reasons (ie. performance) is not
I have a very complex Linq to SQL query that returns a result set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.