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

  • SEARCH
  • Home
  • 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 8294845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:22:09+00:00 2026-06-08T14:22:09+00:00

MVC3 project. I have several classes: Account, Address, Phone etc. which I set up

  • 0

MVC3 project.

I have several classes: Account, Address, Phone etc. which I set up in a view model

namespace ViewModels
{
  public class AccountVM
  {
    public Account Account { get; set; }
    public Address Address { get; set; }
    public Phone Phone { get; set; }       }

In the controller GET action I just call the view

  public ActionResult Create()
  { return View(); }

In the View I pass the View Model

@model AccountVM

I then use @Html.EditorFor’s to populate all the fields and successfully pass it to the POST Action and create the records in the db. So all that code is working.

@Html.EditorFor(z => z.Account.Number)

The problem arises when I try and pre-populate some of the properties. I do the following in the GET action.

    public ActionResult Create()
    { var viewModel = new AccountVM();
      viewModel.Account.Number = 1000000;
      return View(viewModel); }

The code passes Intellisense but when I run I get the “NullReferenceException was unhandled by user code – Object reference not set to an instance of an object” error.

I get the same error if I try and populate using code in the View.

@{ Model.Account.Number = 1000000; }

I need to be able to programatically populate properties in both the controller and the View. I’ve read several SO posts on how to populate a view model in the controller and modeled my code on them but for some reason my code is not working. What am I’m doing wrong here? How should I go about it in both the Controller and the View? I get that the objects are null when created but can’t figure out how to get around that.

Thanks

  • 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-06-08T14:22:12+00:00Added an answer on June 8, 2026 at 2:22 pm

    You’ve instantiated the VM, but not its Account property… try this:

    public ActionResult Create()
    {
        var viewModel = new AccountVM();
        viewModel.Account = new Account();
        viewModel.Account.Number = 1000000;
        return View(viewModel);
    }
    

    The same goes for the view:

    @{
        if (Model.Account == null) {
            Model.Account = new Account();
        }
        Model.Account.Number = 1000000;
    }
    

    Though there are few times that this probably belongs in the view. It looks like something that should be set in the controller instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an Mvc3 project I have a model class which has a field name
I have a Create View page in my MVC3 project to create instances of
I am writing a MVC3 project. Right now I have a table which has
I am writing a MVC3 project. Right now I have a table which has
I have an MVC3 project for which I use NinjectMVC3 for dependency injection -
I have a very simple .NET MVC3 project set up using jquery mobile for
I have created a new MVC3 project and used the ADO.NET Entity Data Model
I have a custom ValidationAttribute in my ASP.NET MVC3 project which has two conditions
For a current MVC3 project I have a model that has multiple pages for
I have a new MVC3 project with one Controller called PublicController.cs which contains 4

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.