2016-09-12 14 views
0

ユーザーはいくつかの会社を選択でき、いくつかの会社を選択した後、私はbuttongをクリックして何かを行うことができます。 問題は、選択した会社をViewModelでバインドできなかったことです。ここで ASP.Net MVCで選択された項目を取得

は、私が持っているものです。

のViewModel:

public class ParametrosPesquisaViewModel 
{ 
    public ParametrosPesquisaViewModel() 
    {    
     Empresas = new List<EmpresaViewModel>();   
    } 

    public IList<EmpresaViewModel> Empresas { get; set; }  
} 

コントローラー:

をゲット

public ActionResult Pesquisa() 
    { 
     ParametrosPesquisaViewModel parametros = new ParametrosPesquisaViewModel(); 
     var empresas = _empresaAppService.BuscarEmpresasValidas();    

     foreach (EmpresaViewModel empresa in empresas) 
     { 
      //Simulando a empresa logada     
      empresa.Selecionada = empresa.empresa == 1;         
      parametros.Empresas.Add(empresa); 
     } 

     return View(parametros); 
    } 

ポスト

public ActionResult Pesquisa(ParametrosPesquisaViewModel parametros) 
{ 
    //Do something 
} 

HTML:ポストのActionResultで

@model LMX.RecuperadorCupomFiscal.Application.ViewModels.ParametrosPesquisaViewModel 

     @using (Html.BeginForm("Pesquisa", "RecuperadorCupomFiscal", FormMethod.Post)) 
     { 
    ... skipping some html code 
        <table class="table table-striped table-hover"> 
         <thead> 
          <tr> 
           <th> 
            <label for="Empresa">Empresa(s):</label> 
           </th> 
           <th> 
            <label for="cidade_emp">Cidade</label> 
           </th> 
           <th> 
            <label for="estado_emp">Estado</label> 
           </th> 
          </tr> 
         </thead> 
         <tbody> 
          @foreach (var empresa in Model.Empresas) 
          {        
           <tr> 
            <td>          
             @Html.CheckBoxFor(model => empresa.Selecionada)            
             @Html.DisplayFor(model => empresa.NomeEmpresaCodigo)          

            </td> 
            <td> 
             @Html.DisplayFor(model => empresa.cidade_emp) 
            </td> 
            <td> 
             @Html.DisplayFor(model => empresa.estado_emp) 
            </td> 
           </tr>        
          } 
         </tbody> 
        </table> 


     } 

、parametros.Empresasは、バインディングを取得されていません。 この場合の最適なアプローチは何ですか?

+0

これを処理するにはエディタテンプレートを使用できます。 「HttpPost Createアクションメソッド内から選択したチェックボックスを知るには?」(http://stackoverflow.com/questions/38961222/how-to-know-the-selected-checkboxes-from-within-the -httppost-create-action-metho) – Shyju

答えて

2

チェックボックスを生成する方法では、モデルバインダーは送信されたデータをバインドできません。代わりに、ループを次のように書き換えてみてください。

@for (var i = 0; i < Model.Empresas.Count; i++) 
{ 
    <tr> 
     <td> 
      @Html.CheckBoxFor(model => Model.Empresas[i].Selecionada) 
      @Html.DisplayFor(model => Model.Empresas[i].NomeEmpresaCodigo) 
     </td> 
     <td> 
      @Html.DisplayFor(model => Model.Empresas[i].cidade_emp) 
     </td> 
     <td> 
      @Html.DisplayFor(model => Model.Empresas[i].estado_emp) 
     </td> 
    </tr> 
} 
+0

これは、マイクロソフトが「固定」している時間です。誰もが間違いを犯しているので、実装が間違っていなければならないと思います。 – Liam

+0

@ sachin、あなたはその点を持っています。ありがとうございました – Maturano

関連する問題