无法使DropDownList在.NETC#中工作

问题描述

我对.NET还是很陌生,但是我想我已经读完了有关该主题的所有内容(包括关于SO的类似问题,这是我尝试过的东西)。我觉得我已经尝试了所有可能的方法,但仍然无法正常工作。

我有一个Note类和一个Category类。非常简单,每个注释都有一个Category属性,因此我想在显示类别的Create视图中有一个下拉列表。我可以得到一个列表,以正确显示类别名称,仅此而已。它一直告诉我,当出现肯定的时候,我的ViewData中没有IEnumerable,叫做“ Categories”,肯定是1000%...

NoteController中的Create动作如下:

        // GET: Create
        public ActionResult Create()
        {
            SelectList items = (new CategoryService()).GetCategories().Select(c => new SelectListItem
            {
                Value = c.CategoryId.ToString(),Text = c.Name
            }) as SelectList;
            ViewData["Categories"] = items;
            return View();
        }

我已经尝试了视图的一些变化:

@Html.DropDownListFor(e=>e.CategoryId,(IEnumerable<SelectListItem>) ViewData["Categories"])
@Html.DropDownList("Categories","Select a Category")

“我的创建”视图使用一个具有以下内容的NoteCreate模型:

public class NoteCreate {
    ...
    [Display(Name = "Category")]
    [Required]
    public string CategoryId { get; set; }

我的NoteService有一个CreateNote方法,如下所示:

public bool CreateNote(NoteCreate model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                bool isValid = int.TryParse(model.CategoryId,out int id);
                if (!isValid)
                {
                    id = 0;
                }
                var entity =
                new Note()
                {
                    OwnerId = _userId,Title = model.Title,Content = model.Content,CreatedUtc = DateTimeOffset.Now,Status = model.Status,CategoryId = id
                };

                ctx.Notes.Add(entity);
                return ctx.SaveChanges() == 1;
            }
        }

我认为我必须为了下拉列表而将ID转换为字符串(因为SelectListItem的Value和Text是字符串),这就是为什么我在这里将其解析为int的原因

我尝试将列表附加到ViewBag,并且尝试了DropDownListFor和DropDownList的变体

这些组合之一导致一个下拉列表实际显示,我不记得它是什么,但是选择一个项目会导致将null传递给NoteCreate方法(model.CategoryId)

有人可以帮助我吗,可能还有很多其他人会因为文档如此糟糕而在将来为此苦苦挣扎?

更新

我的控制器已重构为:

        // GET: Create
        public ActionResult Create()
        {
            List<SelectListItem> li = new List<SelectListItem>();

            List<Category> Categories = (new CategoryService()).GetCategories().ToList();
            var query = from c in Categories
                        select new SelectListItem()
                        {
                            Value = c.CategoryId.ToString(),Text = c.Name
                        };
            li = query.ToList();
            ViewBag.Categories = li;
            return View();
        }

我的观点已经重构为:

@Html.DropDownList("Categories",ViewBag.Categories as SelectList,new { @class = "form-control" })

这更近了,因为我现在可以加载视图并在下拉列表中查看类别名称。但是,当我保存时,CreateNote方法中的model.CategoryId为null,因此实际上并没有将CategoryId值从下拉列表传递到模型中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)