为什么 ListBoxFor 不选择项目,但 ListBox 是? [英] Why is ListBoxFor not selecting items, but ListBox is?

查看:9
本文介绍了为什么 ListBoxFor 不选择项目,但 ListBox 是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为有以下代码:

<%= Html.ListBoxFor(c => c.Project.Categories,
        new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>

<%= Html.ListBox("MultiSelectList", 
        new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>

唯一的区别是第一个助手是强类型的(ListBoxFor),并且它无法显示选定的项目(1,2),即使项目出现在列表中,等等.更简单的 ListBox 按预期工作.

The only difference is that the first helper is strongly typed (ListBoxFor), and it fails to show the selected items (1,2), even though the items appear in the list, etc. The simpler ListBox is working as expected.

我显然在这里遗漏了一些东西.我可以使用第二种方法,但这真的很困扰我,我想弄清楚.

I'm obviously missing something here. I can use the second approach, but this is really bugging me and I'd like to figure it out.

供参考,我的模型是:

public class ProjectEditModel
{
    public Project Project { get; set; }
    public IEnumerable<Project> Projects { get; set; }
    public IEnumerable<Client> Clients { get; set; }
    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Tag> Tags { get; set; }
    public ProjectSlide SelectedSlide { get; set; }
}

更新

我刚刚将 ListBox 名称更改为 Project.Categories(与我的模型匹配),现在无法选择该项目.

Update

I just changed the ListBox name to Project.Categories (matching my model) and it now FAILS to select the item.

<%= Html.ListBox("Project.Categories",
        new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>

我显然不明白这里发生的魔法.

I'm obviously not understanding the magic that is happening here.

好吧,这纯粹是命名,例如,这行得通……

Ok, this is purely naming, for example, this works...

<%= Html.ListBox("Project_Tags",
new MultiSelectList(Model.Tags, "Id", "Name", Model.Project.Tags.Select(t => t.Id)))%>

...因为字段名称是 Project_Tags,而不是 Project.Tags,实际上,除了 Tag 或 Project.Tags 之外的任何内容都可以使用.我不明白为什么这会导致问题(除了它与实体名称匹配),而且我在这方面做得不够好,无法深入了解.

...because the field name is Project_Tags, not Project.Tags, in fact, anything other than Tags or Project.Tags will work. I don't get why this would cause a problem (other than that it matches the entity name), and I'm not good enough at this to be able to dig in and find out.

推荐答案

我自己偶然发现了这个问题,最后我意识到问题是命名约定.

I've stumbled across this problem myself, finally I realized that the problem was a naming convention.

您不能将包含 SelectList 或 MultiSelectList 的 ViewBag 或 ViewData 属性命名为与包含所选项目的属性模型相同的名称.如果您使用 ListBoxFor 或 DropDownListFor 帮助器,至少不会.

You cannot name the ViewBag or ViewData poperty containing the SelectList or MultiSelectList to the same name your property model containing the selected items. At least not if you're using the ListBoxFor or DropDownListFor helper.

这是一个例子:

    public class Person
    {
          public List<int> Cars { get; set; }
    }

    [HttpGet]
    public ActionResult Create()
    {
          //wont work
          ViewBag.Cars = new SelectList(carsList, "CarId", "Name"); 

          //will work due to different name than the property.
          ViewBag.CarsList = new SelectList(carsList, "CarId", "Name"); 

          return View();
    }

    //View
    @Html.ListBoxFor(model => model.Cars, ViewBag.CarsList as SelectList)

我相信还有很多其他方法可以做到这一点,但它解决了我的问题,希望它对某人有所帮助!

I'm sure theres plenty of other ways doing this, but it solved my problem, hope it will help someone!

这篇关于为什么 ListBoxFor 不选择项目,但 ListBox 是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆