列表框的多项选择是不是在asp.net mvc4工作 [英] Multiple selection of ListBox is not working in asp.net mvc4

查看:251
本文介绍了列表框的多项选择是不是在asp.net mvc4工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从实体框架绑定列表框的数据。我已经在列表框中选择多个值。但是,这些值不会被触发。该物业的数量仅为0。我使用下面的code:

I have bound listbox data from entity framework. I have selected multiple values in that listbox. But, the values are not fired. The count of the property is 0 only. I am using the code below:

public class Sample1  
{  
[Key]  
public int SampleId{ get; set; }  
public string SampleDesc{ get; set; }  
}
public class ExpModel  
{  
public List<Sample1> Sample{ get; set; }  
}
public ActionResult Index()  
{       
ViewData["SampleList"] = new List<Sample1>(entity.samp);  
return View();  
} 
@Html.ListBoxFor(model => model.Sample, new  SelectList(((List<Details.Models.Sample1>)ViewData["SampleList"]), "SampleId", "SampleDesc")) 

我有什么做的?请帮我...

What do I have to do? Please help me...

推荐答案

您应该ListBoxFor帮手绑定到简单/标值,如字符串或整数集合的属性:

You should bind the ListBoxFor helper to a property that is a collection of simple/scalar values such as strings or integers:

public class Sample1  
{  
    [Key]  
    public int SampleId { get; set; }  
    public string SampleDesc { get; set; }  
}

public class ExpModel  
{  
    public List<int> SelectedSampleIds { get; set; }  
}

和则:

public ActionResult Index()  
{       
    ViewData["SampleList"] = new List<Sample1>(entity.samp);
    return View();  
}

和在您的视图:

@Html.ListBoxFor(
    model => model.SelectedSampleIds, 
    new SelectList(
        (List<Details.Models.Sample1>)ViewData["SampleList"], 
        "SampleId", 
        "SampleDesc"
    )
)

现在,当您提交表单,在 SelectedSampleIds 集合将包含所选IDS在列表框中。

Now when you submit the form, the SelectedSampleIds collection will contain the selected ids in the list box.

这篇关于列表框的多项选择是不是在asp.net mvc4工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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