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?

4

1 回答 1

0

Create重新显示视图时,您似乎传递了错误的模型。

尝试传递ord类型的实例,ordersModel这是您的视图被强类型化的对象:

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");
    }
}

更新:

现在您已经展示了您的AddProducts操作,尽管您的视图需要一个ordersModel实例,但您并没有将任何模型传递给视图。所以不要只是return View();。您需要传递一个实例ordersModel

if (or.Count == 1)
{
    ViewData["products_in_list"] = prods;
    ViewData["order"] = or[0];
    ordersModel ord = new ordersModel(or[0]);
    return View(ord);
}
于 2012-05-01T16:57:19.037 回答