【问题标题】:How to display foreign key value from a model to a cascaded dropdown list in mvc5?如何将模型中的外键值显示到 mvc5 中的级联下拉列表?
【发布时间】:2017-02-17 10:25:55
【问题描述】:

我还是 mvc5 的新手,对 javascript 知之甚少。我知道我的级联工作,但我在显示部分遇到问题。我想要做的是有 2 个下拉列表。 1 包含所有客户名称,第 2 将包含他们的待处理交易列表。基本上,第一个下拉菜单将包含客户名称,而第二个将包含视频标题。请检查我做错的代码。为什么我不能显示视频标题。顺便说一句,我在我的交易中尝试了其他属性,我可以显示它们。这就是我知道我的级联不是问题的方式。

型号

//Transaction Model
TransactionID { get; set; }
public int CustomerID { get; set; }
public virtual Customers CustomerName { get; set; }
public int VideoID { get; set; }
public virtual Videos Videos { get; set; }
public int Quantity { get; set; }
[ReadOnly(true)]
public DateTime? TransactionDate { get; set; }
[ReadOnly(true)]
public DateTime? DueDate { get; set; }
[ReadOnly(true)]
public Decimal Cost { get; set; }
[ReadOnly(true)]
public String ReturnStatus { get; set;}

//Customers Model
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public string CustomerContact { get; set; }

//Video Model
public int VideoID { get; set; }
public string VideoTitle { get; set; }
public int CategoryID { get; set; }
public virtual Category VideoCategory { get; set; }
[Range(0,99)]
public int VideoIn { get; set; }
[Range(0,99)]
public int VideoOut { get; set; }

控制器

public ActionResult CustomerList()
{
   var customers = db.Customers.OrderBy(x => x.CustomerID).ToList();
    if (HttpContext.Request.IsAjaxRequest())
    {
        return Json(new SelectList(
                    customers,
                    "CustomerID",
                    "CustomerName"), JsonRequestBehavior.AllowGet
                    );
    }
    return View(customers);
}

public ActionResult Transact(int cusId)
{
    var transact = db.Transactions
                     .Where(x => x.CustomerID == cusId)
                     .Where(s => s.ReturnStatus == "FALSE").ToList();
    if (HttpContext.Request.IsAjaxRequest())
        return Json(new SelectList(
                        transact,
                        "TransactionID",
                        "VideoTitle"), JsonRequestBehavior.AllowGet
                    );
    return View(transact);
}

【问题讨论】:

  • 你得到值交易变量了吗?
  • 您的Transaction 模型甚至不包含名为VideoTitle 的属性。而且您甚至没有显示视图代码。并在JsonResult 中返回一个匿名对象,而不是SelectList(将SelectListItem 的额外值传递给客户端是没有意义的。
  • @LateshtClick.com 是的,兄弟。感谢您的回复。
  • @StephenMuecke 感谢您提供后来的信息。

标签: javascript c# json asp.net-mvc asp.net-mvc-5


【解决方案1】:

经过数小时的错误尝试,我找到了解决方案。我所做的是在查询中添加了一个联接。

var transact = db.Transactions
                        .Where(x => x.CustomerID == cusId)
                        .Where(s => s.ReturnStatus == "FALSE")
                        .Join(db.Videos,
                        v => v.VideoID,
                        t => t.VideoID,
                        (transaction, videos) => new { 
                        TransactionID = transaction.TransactionID,
                        VideoTitle = videos.VideoTitle }).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 2020-05-14
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2020-12-23
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多