【问题标题】:Can not load book follow by category id in dropdownlist using javascript无法使用 javascript 在下拉列表中按类别 ID 加载图书
【发布时间】:2021-12-23 17:34:33
【问题描述】:

我在选择每个类别时遇到问题,它无法显示每个类别的书籍。我的索引代码在这里

@using (Html.BeginForm("Index", "BorrowBook", FormMethod.Post, new { name = "demoForm" }))
        {
            @Html.DropDownList("id_category", (IEnumerable<SelectListItem>)ViewBag.listTest, "-- Select Category --",
                 new { onchange = "SelectedIndexChanged()" })
<div> Show list of book in the middle </div>
}

我的控制器就是这样

// GET: BorrowBook
        public ActionResult Index(string mess)
        {
            var query = context.categories.Select(c => new { c.id_category, c.name });
            var userInfomatiom = (LibraryAsp.Models.User)Session["USER"];
            if (userInfomatiom.Role.id_role == 1)
            {
                ViewBag.listP = publisherDao.getAll();
                ViewBag.listC = categoryDao.getAll();
                ViewBag.list = bookDao.getAll();
                ViewBag.listTest = new SelectList(query.AsEnumerable(), "id_category", "name");
                ViewBag.mes = mess;
                return View();
            }
            else
            {
                return RedirectToAction("Error", "Home");
            }
         }

        [HttpPost]
        public ActionResult Index (Category category)
        {
            ViewBag.listP = publisherDao.getAll();
            ViewBag.listC = categoryDao.getAll();
            ViewBag.list = context.books.Where(p => p.id_category == category.id_category).ToList();
            return View();
        }

调试的时候发现id分类、表单或者值是这样加载的 但是当我选择每个类别时,我会遇到这样的错误: 我认为我的 java 脚本有问题。但我无法弄清楚这一点。任何人都可以帮助我,非常感谢。

【问题讨论】:

  • 问题解决了,我忘记传数据了。

标签: javascript c# asp.net .net


【解决方案1】:

问题解决了,我忘记了传递数据。应该是这样的

[HttpPost]
        public ActionResult Index (Category category)
        {
            var query = context.categories.Select(c => new { c.id_category, c.name });
            ViewBag.listP = publisherDao.getAll();
            ViewBag.listC = categoryDao.getAll();
            ViewBag.list = context.books.Where(p => p.id_category == category.id_category).ToList();
            ViewBag.listTest = new SelectList(query.AsEnumerable(), "id_category", "name");
            return View();
        }

【讨论】:

    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多