在选择值挑战ListBoxFor [英] Challenges with selecting values in ListBoxFor

查看:132
本文介绍了在选择值挑战ListBoxFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,它可能是一个贫穷的人,所以请温柔。

This is my first question, and it is probably a poor one, so please be gentle.

最近在我的第一个ASP.Net MVC2 Web应用程序的工作,我的一些问题来了,当我需要在列表框中选择多个值。我工作围绕它与一些jQuery,但说干就干,把一些很简单的code证明。我使用的是EF为模型,拥有两个实体 - 客户和HelpDeskCalls:

Working on my first ASP.Net MVC2 web app recently, I came across some issues when I needed to select multiple values in a list box. I worked around it with some jQuery, but went ahead and put together some very simple code to demonstrate. I'm using EF for the model, with two entities - Customers and HelpDeskCalls:

控制器:

 public ActionResult Edit(int id)
    {
        Customer currCustomer = ctx.Customers.Include("HelpDeskCalls").Where(c => c.ID == id).FirstOrDefault();
        List<HelpDeskCall> currCustCalls = (ctx.HelpDeskCalls.Where(h => h.CustomerID == id)).ToList();
        List<SelectListItem> currSelectItems = new List<SelectListItem>();
        List<String> selectedValues = new List<string>();
        foreach (HelpDeskCall currCall in currCustCalls)
        {
            bool isSelected = (currCall.ID % 2 == 0) ? true : false;
            //Just select the IDs which are even numbers...
            currSelectItems.Add(new SelectListItem() { Selected = isSelected, Text = currCall.CallTitle, Value = currCall.ID.ToString() });
            //add the selected values into a separate list as well...
            if (isSelected)
            {
                selectedValues.Add(currCall.ID.ToString());
            }
        }
        ViewData["currCalls"] = (IEnumerable<SelectListItem>) currSelectItems;
        ViewData["currSelected"] = (IEnumerable<String>) selectedValues;
        return View("Edit", currCustomer);
    }

查看:

<div class="editor-field">
            <%: Html.ListBoxFor(model => model.HelpDeskCalls, new MultiSelectList(Model.HelpDeskCalls, "ID", "CallTitle", (IEnumerable) ViewData["currSelected"]), new { size = "12" })%>       
            <%: Html.ListBoxFor(model => model.HelpDeskCalls, ViewData["currCalls"] as IEnumerable<SelectListItem>, new { size = "12"}) %>
            <%: Html.ListBox("Model.HelpDeskCalls", new MultiSelectList(Model.HelpDeskCalls, "ID", "CallTitle", (IEnumerable)ViewData["currSelected"]), new { size = "12"}) %>      
            <%: Html.ValidationMessageFor(model => model.HelpDeskCalls) %>
</div>

有关此示例中,我只是选择HelpDeskCall.IDs这是偶数。使用SelectListItems的IEnumerable One使用一个IEnumerable值的选择,一:我想两个不同的语法来ListBoxFor。默认情况下,当我运行这个code,没有选择,其一是ListBoxFor制造,但非强类型的列表框会正确选择。

For this sample, I'm just selecting HelpDeskCall.IDs which are even. I'm trying two different syntaxes for ListBoxFor: One uses an IEnumerable of values for selections, one using an IEnumerable of SelectListItems. By default, when I run this code, no selections are made to either ListBoxFor, but the non-strongly typed ListBox selects correctly.

我看了在ASP.Net和这个职位 <一个href=\"http://stackoverflow.com/questions/2308846/why-is-listboxfor-not-selecting-items-but-listbox-is\">this螺纹对SO,但没有喜悦。事实上,如果我添加了重写的ToString()我HelpDeskCall类(如ASP.net线程建议)所有值都被选中,这是不正确的无论是。

I read this post on ASP.Net and this thread on SO, but no joy. In fact, if I add the override ToString() to my HelpDeskCall class (as suggested in the ASP.net thread) all values are selected, which isn't right either.

如果有人能阐明应如何工作的一些光(和什么我丢失或做错了),那么这将新手很感激。

If someone could shed some light on how this should work (and what I'm missing or doing wrong), this then neophyte would be very grateful.

约翰

推荐答案

下面是说明强类型版本的例子:

Here's an example illustrating the strongly typed version:

型号:

public class MyViewModel
{
    public int[] SelectedItemIds { get; set; }
    public MultiSelectList Items { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // Preselect items with id 1 and 3
        var selectedItemIds = new[] { 1, 3 };

        var model = new MyViewModel
        {
            Items = new MultiSelectList(
                new[] 
                {
                    // TODO: Fetch from your repository
                    new { Id = 1, Name = "item 1" },
                    new { Id = 2, Name = "item 2" },
                    new { Id = 3, Name = "item 3" },
                }, 
                "Id", 
                "Name", 
                selectedItemIds
            )
        };

        return View(model);
    }
}

查看:

<%: Html.ListBoxFor(x => x.SelectedItemIds, Model.Items) %>

这篇关于在选择值挑战ListBoxFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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