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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:20:54+00:00 2026-06-03T10:20:54+00:00

I have created an entity data model from my database. however in certain areas

  • 0

I have created an entity data model from my database. however in certain areas of the application i need to pass two models. thus i create a third model which has as properties the objects of each required model.

In the scenario, i want to use one model just to show some data to the user and the other is to be populated by the user using form elements. therefore, i create a constructor in my custom model to populate it. here’s the code:

THE CUSTOM MODEL

public class ordersModel
{
    public ordersModel(order or)
    {
        this.prods = new order_products();
        this.new_order = new order();
        this.new_order.customer_id = or.customer_id;
        this.new_order.my_id = or.my_id;
        this.new_order.my_order_id = or.my_order_id;
        this.new_order.order_date = or.order_date;
        this.new_order.order_status_id = or.order_status_id;
    }
    public order new_order { get; set; }
    public order_products prods { get; set; }
}

IT IS USED IN THE CONTROLLER AS FOLLOWS:

public ActionResult Create()
    {
        order or = new order();
        // Store logged-in user's company id in Session
        //or.my_id = Session["my_id"].ToString();
        //do something to allow user to select customer, maybe use ajax
        or.customer_id = "123";
        or.order_amount = 0;
        or.my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
        // display date picker to select date
        or.order_date = DateTime.Now.Date;
        // fetch statuses from database and show in select list box
        or.order_status_id = 1;
        return View(or);
    } 

    //
    // POST: /Orders/Create

    [HttpPost]
    public ActionResult Create(order or)
    {
        using (invoicrEntities db = new invoicrEntities())
        {
            var temp = db.last_order_number.SingleOrDefault(p => p.my_id == or.my_id);
            if (temp != null)
            {
                or.my_order_id = temp.my_order_id + 1;
                if (ModelState.IsValid)
                {
                    ordersModel ord = new ordersModel(or);

                    db.orders.AddObject(or);
                    temp.my_order_id = temp.my_order_id + 1;
                    //TempData["my_order_id"] = or.my_order_id;
                    db.SaveChanges();
                    return RedirectToAction("AddProducts", ord);
                    //return RedirectToAction("AddProducts", new { id = or.my_order_id });
                }
                return View(or);
            }
            return RedirectToAction("someErrorPageDueToCreateOrder");
        }
    }

    public ActionResult AddProducts()
    {
        using (invoicrEntities db = new invoicrEntities())
        {
            //string my_id = TempData["my_id"].ToString();
            //string my_order_id = TempData["my_order_id"].ToString();

            string my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
            int my_order_id = 1;
            //Int64 my_order_id = Convert.ToInt64(RouteData.Values["order_id"]);

            // Display this list in the view
            var prods = db.order_products.Where(p => p.my_id == my_id).Where(p => p.my_order_id == my_order_id).ToList();

            var or = db.orders.Where(p => p.my_id == my_id).Where(p => p.my_order_id == my_order_id).ToList();
            if (or.Count == 1)
            {
                //ViewData["name"] = "sameer";
                ViewData["products_in_list"] = prods;
                ViewData["order"] = or[0];
                return View();
            }
            return RedirectToAction("someErrorPageDueToAddProducts");
        }
    }

    [HttpPost]
    public ActionResult AddProducts(order_products prod)
    {
        prod.my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
        // find a way to get the my_order_id
        prod.my_order_id = 1;
        return View();
    }

THIS ALL WORKS OUT WELL, UNTIL IN THE “ADDPRODUCTS” VIEW:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<invoicr.Models.ordersModel>" %>

AddProducts

<h2>AddProducts</h2>
<%: Model.new_order.my_id %>

the above statement gives an error

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

what am i doing wrong here?

  • 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-03T10:21:09+00:00Added an answer on June 3, 2026 at 10:21 am

    You seem to be passing the wrong model when redisplaying your Create view.

    Try passing the ord instance which is of type ordersModel and which is what your view is strongly typed to:

    public ActionResult Create(order or)
    {
        using (invoicrEntities db = new invoicrEntities())
        {
            var temp = db.last_order_number.SingleOrDefault(p => p.my_id == or.my_id);
            if (temp != null)
            {
                or.my_order_id = temp.my_order_id + 1;
                ordersModel ord = new ordersModel(or);
                if (ModelState.IsValid)
                {
                    db.orders.AddObject(or);
                    temp.my_order_id = temp.my_order_id + 1;
                    db.SaveChanges();
                    return RedirectToAction("AddProducts", ord);
                }
                return View(ord);
            }
            return RedirectToAction("someErrorPageDueToCreateOrder");
        }
    }
    

    UPDATE:

    Now that you have shown your AddProducts action you are not passing any model to the view although your view expects an ordersModel instance. So don’t just return View();. You need to pass an instance of ordersModel:

    if (or.Count == 1)
    {
        ViewData["products_in_list"] = prods;
        ViewData["order"] = or[0];
        ordersModel ord = new ordersModel(or[0]);
        return View(ord);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an entity data model and generated a database from it. One
I have created a repository that is returning data from my database using Entity
I have an Entity Data Model that I have created, and its pulling in
I have created a Dynamic Data site against an Entity Framework Model I have
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I've created a new ASP.NET website. I've generated an Entity Data Model from my
Let's say I have a northwind database and I use ADO.NET Entity Data Model
Is it possible to create Entity Data Model (edmx file) from MS Access Database
I have created a Entity Framework 4 model with Visual Studio 2010 and generated
I have an existing SQL Server database, where I store data from large specific

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.