如何显示多个与asp.net MVC 3和ListBoxFor选择? [英] How to show multiple selected with asp.net mvc 3 and ListBoxFor?

查看:101
本文介绍了如何显示多个与asp.net MVC 3和ListBoxFor选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个VM属性。

  public IList<Guid> SelectedEligiableCategories { get; set; }
  public IList<SelectListItem> EligiableCategories { get; set; }  

我有这样的助手在我看来

I have this helpers in my view

  @Html.LabelFor(x => x.EligibleCategoryFrmVm.SelectedEligiableCategories, "Eligible Categories:")
    @Html.ListBoxFor(x => Model.EligibleCategoryFrmVm.SelectedEligiableCategories, Model.EligibleCategoryFrmVm.EligiableCategories, new { @class = "eligibleCategoryListBox" }) 

我有这样的code在我的控制器

I have this code in my controller

  List<SelectListItem> eligibleCategoriesListItems = Mapper.Map<List<EligibleCategory>, List<SelectListItem>>(eligibleCategories);
  foreach (var rewardTier in creditCard.RewardTiers)
        {
            CbRewardTierFrmVm rewardTierFrmVm = new CbRewardTierFrmVm();
            rewardTierFrmVm.EligibleCategoryFrmVm.EligiableCategories = eligibleCategoriesListItems;

            foreach (var ec in rewardTier.EligibleCategories)
            {
                rewardTierFrmVm.EligibleCategoryFrmVm.SelectedEligiableCategories.Add(ec.Id);
            }

            vm.CbRewardTierFrmVm.Add(rewardTierFrmVm);
        }

然而,当我打开了我的观点。我的列表框的值都没有被选中。我不知道为什么。如果这是一个选择列表的这会工作,因为这将在SelectedEligiableCategories匹配到列表中的值

Yet when I load up my view. None of values for my ListBox are selected. I am not sure why. If this was a selectList this would work as it would match up the SelectedEligiableCategories to the value in the list.

我不知道这是否是因为有多个选择

I am not sure if this is because there is multiple selects

修改

<select name="CbRewardTierFrmVm[63b504c0-0f9a-47ba-a8ff-db85f48d5f0f].EligibleCategoryFrmVm.SelectedEligiableCategories" multiple="multiple" id="CbRewardTierFrmVm_63b504c0-0f9a-47ba-a8ff-db85f48d5f0f__EligibleCategoryFrmVm_SelectedEligiableCategories" data-val-required="Must choose at least one eligible category." data-val="true" class="eligibleCategoryListBox ui-wizard-content ui-helper-reset ui-state-default" style="display: none;">
   <option value="ed2bb5f9-4565-4f69-ab15-9fca011c0692">Gas</option>
</select>

你觉得这是因为我使用的http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

EDIT2

我已先行作出了榜样。我必须失去了一些东西(不知道)。当我使用达林季米特洛夫它的工作原理。

I gone ahead and make an example. I must be missing something(not sure what). When I use "Darin Dimitrov" it works.

我切换到一张下拉,因为我用它得到了同样的问题也是如此。

I switched the example to a dropdown as I am getting the same problem with it as well.

在这个例子中,我没有使用视图模型,因为我最初的假设是不知何故,我是从史蒂芬桑德斯使用助手可能会影响它,所以我打算过他的榜样。

In this example I am not using a viewmodel since my initial assumption was somehow the helper I was using from Steven Sanders might be effecting it so I was going off his example.

这似乎并没有出现这种情况,因为我删除,并仍然得到这个问题。

This does not seem to be the case as I removed it and still get this problem.

  public class Gift
    {
        public string Name { get; set; }
        public double Price { get; set; }
        public string SelectedItem { get; set; }
        public IList<SelectListItem> Items { get; set; }
    }


 public ActionResult Index()
    {
        List<SelectListItem> items = new List<SelectListItem>
        {

              new SelectListItem {Value = "",Text ="--"},
              new SelectListItem {Value = "1",Text ="1"},
              new SelectListItem {Value = "2",Text ="2"},
        };


        var initialData = new[] {
            new Gift { Name = "Tall Hat", Price = 39.95, Items = items, SelectedItem = "2" },
            new Gift { Name = "Long Cloak", Price = 120.00, Items = items, SelectedItem = "1"  }
         };

        return View("Index3",initialData);
    }

@model IList<EditorDemo.Models.Gift>

@{
    ViewBag.Title = "Index3";
}

@for (int i = 0; i < Model.Count; i++)
{
     @Html.DropDownListFor(x => x[i].SelectedItem, new SelectList(Model[i].Items, "Value", "Text")) 
}

这似乎并不能够处理,当你把它放在forloop并尝试使多个下拉列表。

It seems to not be able to handle when you put it in forloop and try it make more than one dropdown list.

推荐答案

我下面的作品。

型号:

public class MyViewModel
{
    public IList<Guid> SelectedEligiableCategories { get; set; }
    public IList<SelectListItem> EligiableCategories { get; set; } 
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            SelectedEligiableCategories = new[]
            {
                // preselect the second and the fourth item
                new Guid("35830042-3556-11E1-BCDC-A6184924019B"),
                new Guid("4253876A-3556-11E1-BC17-B7184924019B")
            }.ToList(),

            EligiableCategories = new[]
            {
                new SelectListItem { Value = "2DA62E3A-3556-11E1-8A0A-9B184924019B", Text = "item 1" },
                new SelectListItem { Value = "35830042-3556-11E1-BCDC-A6184924019B", Text = "item 2" },
                new SelectListItem { Value = "3D07EBAC-3556-11E1-8943-B6184924019B", Text = "item 3" },
                new SelectListItem { Value = "4253876A-3556-11E1-BC17-B7184924019B", Text = "item 4" },
            }
        };
        return View(model);
    }
}

查看:

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.ListBoxFor(
        x => x.SelectedEligiableCategories, 
        Model.EligiableCategories,  
        new { @class = "eligibleCategoryListBox" }
    )
}

结果:

更新:

现在,你已经证明为例,允许来说明这个问题,你可以构建时指定所选项目的的SelectList

Now that you have shown an example allowing to illustrate the problem, you could specify the selected item when building the SelectList:

@Html.DropDownListFor(
    x => x[i].SelectedItem, 
    new SelectList(Model[i].Items, "Value", "Text", Model[i].SelectedItem)
)

原因值不是preselected是因为你的DropDownList的绑定属性( X =&GT; X [I] .SelectedItem )的列表而在我的例子中,我用一个简单的属性。

The reason a value was not preselected was because you were binding the dropdownlist to a list of properties (x => x[i].SelectedItem) whereas in my example I was using a simple property.

如果你想与你可以使用下面的ListBoxFor帮助做到这一点:

And if you wanted to do this with the ListBoxFor helper you could use the following:

@Html.ListBoxFor(
    x => x[i].SelectedItems, 
    new MultiSelectList(Model[i].Items, "Value", "Text", Model[i].SelectedItems)
)

SelectedItems 属性变成采集和我们使用的 MultiSelectList 代替的SelectList

The SelectedItems property becomes a collection and we use a MultiSelectList instead of a SelectList.

这篇关于如何显示多个与asp.net MVC 3和ListBoxFor选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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