我在MVC3應用程序中使用@model是一個可枚舉的索引視圖。在這個模型中,我有一個accountID,我想用它來在視圖過濾器中填充我的下拉列表和帳戶,以便用戶能夠過濾帳戶。在mvc3應用程序中填充下拉列表的最佳方法
這是實現此目標的最佳方法嗎?
在此先感謝。
這是視圖:
@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());
}
你能告訴我們你到底做了什麼/試過了嗎? – jzm