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

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

问题描述

我有以下的code在我看来:

I have the following code in my view:

<%= 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),即使项目出现在列表中,等等。比较简单的列表框按预期工作。

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.

有关参考,我的模型是:

For reference, my model is:

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; }
}

更新

我只是改变了列表框名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,其实比的标签或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.

您不能命名包含选择列表或MultiSelectList同名包含所选项目的属性模型的ViewBag或ViewData的poperty。至少当你使用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不选择项目,但列表框是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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