2015-03-02 22 views
0

因此,我试图通过DropDownList和BeginForm传递选定的多选值。不想用javascript/ajax传递。所选的插件工作正常,向我展示像我想要的条目。但我对控制器获得空值:通过BeginForm和DropDownList传递选定的值

型号

public class SorteioEspecial 
{ 
    RepositoryService service = new RepositoryService(); 

    public SorteioEspecial() 
    { 
     funcionario = new List<Funcionario>(); 
     ponderacaoFuncionario = new List<PonderacaoFuncionario>(); 
     SelectedIds = new List<int>(); 

    } 

    public int Id { get; set; } 
    public IEnumerable<Funcionario> funcionario { get; set; } 
    public IEnumerable<PonderacaoFuncionario> ponderacaoFuncionario { get; set; } 
    public List<int> SelectedIds { get; set; } 

    public IEnumerable<Funcionario> GetFuncionarios() 
    { 
     funcionario = service.GetFuncionarios(); 
     return funcionario; 
    } 

    public IEnumerable<PonderacaoFuncionario> GetPonderacaoFuncionario() 
    { 
     ponderacaoFuncionario = service.GetPonderacaoFuncionario(); 
     return ponderacaoFuncionario; 
    } 


} 

控制器

[HttpGet] 
    public ActionResult EscolherFuncionarios() 
    { 
     var sorteioEspecial = new SorteioEspecial(); 
     List<Funcionario> list = new List<Funcionario>(); 
     list = sorteioEspecial.GetFuncionarios().ToList().OrderBy(x => x.Nome).ToList(); 
     ViewBag.FuncionarioId = new SelectList(list, "Id", "Nome"); 
     return View(sorteioEspecial); 
    } 

    [HttpPost] 
    public ActionResult EscolherFuncionarios(List<int> SelectedIds) 
    { 
     return View(); 
    } 

查看

@model Apdd.Models.SorteioEspecial 

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 


    <h2>Escolha os funcionários a ir a sorteio</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.DropDownList("FuncionarioId", null, htmlAttributes: new { @class = "chosen-select", @data_placeholder = "Pick one!", @multiple = "true" }) 

    <input type="submit" value="save" /> 
} 

<script src="~/Scripts/jquery-2.1.1.js"></script> 
<script src="~/Scripts/jquery.js"></script> 
<script src="~/Scripts/chosen.proto.js"></script> 
<link href="~/Scripts/chosen.css" rel="stylesheet" /> 
<script src="~/Scripts/chosen.jquery.js"></script> 
<script> 
    $(".chosen-select").chosen({ 
     disable_search_threshold: 10, 
     no_results_text: "None!", 
     width: "95%" 
    }); 
</script> 

ViewBag中的值是一个实体列表(Id,Name和其他一些参数),对于我一直在寻找的东西,选择的只是传递Id,正是我想要的。我需要做什么来将值传递给控制器​​?

+0

你可以参考通过.. http://stackoverflow.com/questions/12295199/how-to-pass-multiselect -lists-selected-items-back-to-controller – 2015-03-02 12:33:20

+0

@MilindRajput我已经打开了这个解决方案,但我仍然无法工作 – danielpm 2015-03-02 12:36:33

回答

0

首先所选的项目将以逗号分隔的字符串形式发布,因此您需要将您的下拉列表与字符串属性绑定。

其次你的下拉列表的名字是FuncionarioId所以它会发布在FormCollection该键的同时,你有你的动作参数哪种类型List<int>

的。如果你改变你的行为特征和形式收集检查一下,你会发现逗号分隔FormCollection值:

[HttpPost] 
public ActionResult EscolherFuncionarios(FormCollection form) 
{ 
    string selectedValues = form["FuncionarioId"]; 
    return View(); 
} 

您也可以参考this article of Rick Anderson where he explained how to use Multi Select

0

值被发布到对照oller,bot问题与控制器参数绑定有关,因为参数和下拉列表中的名称不同。如果将控制器参数列表的名称从“SelectedIds”更改为下拉列表名称“FuncionarioId”,它应该可以工作。 因此,改变这种:

[HttpPost] 
public ActionResult EscolherFuncionarios(List<int> SelectedIds) 
{ 
    return View(); 
} 

这样:

[HttpPost] 
public ActionResult Test(List<int> FuncionarioId) 
{ 
    return View(); 
}