在查询中使用选定的下拉列表值

时间:2015-08-20 12:02:18

标签: asp.net-mvc

我正在尝试根据从下拉列表中选取的值将数据插入到sql server临时表中。在我的模型中,我有4个类:

    public class Customer
    {
        [Key]
        public int CustID { get; set; }
        public string CustName { get; set; }

        public virtual ICollection<Sale> Sales { get; set; }
    }
}

     public class Product
    {

        [Key]
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public decimal Price { get; set; }
        public virtual ICollection<Sale> Sales { get; set; }
    }
}

        public class Sale
    {
        [Key]
        public int RecordID { get; set; }
        public int ProductID { get; set; }
        public int CustID { get; set; }
        public int InvoiceNo { get; set; }
        public int Quantity { get; set; }

        public virtual Customer Customer { get; set; }
        public virtual Product Product { get; set; }

    }

        public class TempTable
    {
        [Key]
        public int recordID { get; set; }
        public string CustName { get; set; }
        public string PoductName { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }
        public int InvoiceNo { get; set; }

    }

在我的SaleController中,我有以下行动:

            public ActionResult OldInvoice(Sale sale)
        {
            ViewBag.InvoiceNo = new SelectList(db.Sales, "RecordID",   "InvoiceNo", sale.RecordID);
            return View();

        }

        [HttpPost]
        public ActionResult OldInvoice(TempTable t, int InvoiceNoToSearch = 0)
        {
            var query = (from s in db.Sales
                         join c in db.Customers on s.CustID equals c.CustID
                         join p in db.Products on s.ProductID equals p.ProductID
                         where s.InvoiceNo.Equals(s.InvoiceNo == InvoiceNoToSearch)
                         select new
                         {
                             s.InvoiceNo,
                             s.Quantity,
                             c.CustName,
                             p.ProductName,
                             p.Price
                         }).ToList();

            foreach (var temp in query)
            {
                t.CustName = temp.CustName;
                t.InvoiceNo = temp.InvoiceNo;
                t.PoductName = temp.ProductName;
                t.Price = temp.Price;
                t.Quantity = temp.Quantity;
                db.TempTables.Add(t);
                db.SaveChanges();
            }
            return RedirectToAction("OldInvoice");
        }

最后我的观点是:

    @model IEnumerable<UsingDropDownListValueInQuery.Models.Sale>
@{
    ViewBag.Title = "OldInvoice";
}

<h2>OldInvoice</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

@Html.DropDownList("InvoiceNo")

   <p>
       <input type="submit" value="Save" />
   </p> 

}

我收到以下错误: 无法转换类型&#39; System.Boolean&#39;键入&#39; System.Object&#39;。 LINQ to Entities仅支持转换EDM原语或枚举类型。

如果我在Where Statement中输入现有发票,那么它可以完成工作但是如果我尝试从下拉列表中选择值

1 个答案:

答案 0 :(得分:0)

问题似乎是你从未实际发回过价值。在您看来,您已经创建了一个名为&#34; InvoiceNo&#34;的选择,但在您的帖子操作中,您没有任何可以接受该值的参数。您想要绑定的最近且可能是InvoiceNoToSearch,但如果您想要使用该变量,则您的视图需要匹配:

@Html.DropDownList("InvoiceNoToSearch")

当然,这意味着SelectList中的ViewBag不再自动绑定为该选项的选项,但只需明确指定即可解决此问题:

@Html.DropDownList("InvoiceNoToSearch", (SelectList)ViewBag.InvoiceNo)