我在 MVC3 应用程序中有一个索引视图,其 @model 是可枚举的。在这个模型中,我有一个 accountID,我想用它在视图过滤器中填充我的下拉列表中的帐户,以便用户能够过滤帐户。
实现这一目标的最佳方法是什么?
提前致谢。
这是视图:
@model IEnumerable<MoneyAdmin.Model.ContaAReceber>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.Partial("_SubmenuAdmin")
<div class="tituloCadastro">
Lista de Contas a Receber
</div>
<div class="buttonContainer novo">
@Html.ActionLink("Nova Conta", "Create")
</div>
<div class="filtros">
@using (Html.BeginForm()) {
<div class="filterField">
<label>Data Inicial:</label>
@Html.TextBox("dataInicial", @DateTime.Now.ToShortDateString())
</div>
<div class="filterField">
<label>Data Final:</label>
@Html.TextBox("dataFinal", @DateTime.Now.ToShortDateString())
</div>
<div class="filterField">
<label>Tipo de Conta:</label>
@Html.DropDownList("contaID")
</div>
<input type="submit" value="Atualizar" />
}
</div>
和控制器方法:
public ViewResult Index(string dataInicial, string dataFinal, string contaID)
{
var crs = from cr in db.contasareceber.Include("contas")
select cr;
if (!string.IsNullOrEmpty(dataInicial) && !string.IsNullOrEmpty(dataFinal))
{
DateTime di = DateTime.Parse(dataInicial);
DateTime df = DateTime.Parse(dataFinal);
crs = crs.Where(cr => cr.dataPagamento >= di && cr.dataPagamento <= df);
}
return View(crs.ToList());
}