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 9254373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:27:09+00:00 2026-06-18T11:27:09+00:00

My view <table> <tr> <td> Middle Name : </td> <td> @Html.EditorFor(model => @Model.EmployeeDetail.MiddleName) </td>

  • 0

My view

<table>
                 <tr>
        <td>
            Middle Name : 
        </td>
        <td>
            @Html.EditorFor(model => @Model.EmployeeDetail.MiddleName)
        </td>
        <td>
            @Html.ValidationMessageFor(model => @Model.EmployeeDetail.MiddleName)
        </td>
    </tr>
    <tr>
        <td>
            Last Name : 
        </td>
        <td>
            @Html.EditorFor(model => @Model.EmployeeDetail.LastName)
        </td>
        <td>
            @Html.ValidationMessageFor(model => @Model.EmployeeDetail.LastName)
        </td>
    </tr>
    <tr>
        <td>
            Date of Birth  : 
        </td>
        <td>
            @Html.EditorFor(model => @Model.EmployeeDetail.DateOfBirth)
        </td>
        <td>
            @Html.ValidationMessageFor(model => @Model.EmployeeDetail.DateOfBirth)
        </td>
    </tr>
</table>

My Controller action

        public ActionResult Index()
    {
        var id = 0;
        if (Session["id"] != null)
        {
            id = Convert.ToInt32((Session["id"].ToString()));
        }
        var empDetails = _empRepository.GetEmployeeDetails(id);
        var emp = new UserViewModel { EmployeeDetail = empDetails };
        return View(emp);
    }

My viewmodel

public class UserViewModel
{
    public EmployeeDetail EmployeeDetail { get; set; }
}

my model

public partial class EmployeeDetail
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public System.DateTime DateOfBirth { get; set; }
    public string IsAdmin { get; set; }
}

In my view I am getting object reference at @Html.ValidationMessageFor(model => @Model.EmployeeDetail.DateOfBirth)

I am getting this error due to viewmodel “emp” which I am passing to view from controller action is null as I dont have any data in database for that particular Id.

How can I avoid this object reference error.

  • 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-18T11:27:10+00:00Added an answer on June 18, 2026 at 11:27 am

    Instead of

     @Html.EditorFor(model => @Model.EmployeeDetail.DateOfBirth)
    

    use

     @Html.EditorFor(model => model.EmployeeDetail.DateOfBirth)
    

    You should do it for all lines…

     @Html.SomethingFor(model => model.EmployeeDetail.Something) // Not @Model.Something
    

    UPDATE:

    Don’t let EmployeeDetail to be null.

    public class UserViewModel
    {
         private EmployeeDetail _employeeDetail = new EmployeeDetail();
    
         public EmployeeDetail EmployeeDetail 
         { 
            get { return _employeeDetail; }
            set { _employeeDetail = value ?? new EmployeeDetail(); } 
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HTML table on the ASP.NET MVC View page. Now I have to
I have a view table which is a union of two separate tables (say
This is for an existing database with a View table. Trying to list a
DDMS views Database only in eclipse. I want to view table contents from CMD.
I have table View with grouped style with three sections. Get information from dictionary
I have a table view of peoples names and when I click on one
I have a table view with numerous cells. When I press one, a tick
I have a table view controller with (among others) a cell that is to
I have a table view with a custom cell ( UISwitch on every cell
I have a table view. And I have multiple rows. While doing the reorder

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.