ユーザーはいくつかの会社を選択でき、いくつかの会社を選択した後、私は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は、バインディングを取得されていません。 この場合の最適なアプローチは何ですか?
これを処理するにはエディタテンプレートを使用できます。 「HttpPost Createアクションメソッド内から選択したチェックボックスを知るには?」(http://stackoverflow.com/questions/38961222/how-to-know-the-selected-checkboxes-from-within-the -httppost-create-action-metho) – Shyju