视图中的两个模型 - 不适合我

时间:2012-05-01 16:53:43

标签: asp.net-mvc

我已经从我的数据库创建了一个实体数据模型。但是在应用的某些领域我需要通过两个模型。因此,我创建了第三个模型,它具有每个所需模型的对象作为属性。

在该场景中,我想使用一个模型来向用户显示一些数据,另一个模型由用户使用表单元素填充。因此,我在自定义模型中创建一个构造函数来填充它。这是代码:

定制模型

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

它在控制器中使用如下:

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

这一切都运作良好,直到“ADDPRODUCTS”观点:

<%@ 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 %>

以上陈述给出了错误

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

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

尝试传递类型为ord的{​​{1}}实例,这是您的视图强类型的实例:

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