2015-03-02 24 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(); 
}