因此,我想获取下拉列表的值(== 帐户 ID)以插入新交易,您可以在下面找到我的视图:
<div id="content">
<h2>Overschrijving</h2>
<p id="topmsg">velden met een * zijn verplicht</p><p> </p>
<dl class="clearfix form">
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.ValidationSummary(true, "Gelieve alles correct in te vullen.")
<dt>@Html.Label("Rekening: *")</dt>
<dd>@Html.DropDownList("ddlAccounts")</dd>
<dt>@Html.Label("Begunstigde: *")</dt>
<dd>@Html.TextBox("ToAccountNumber")</dd>
<dt>@Html.Label("Bedrag: *")</dt>
<dd>@Html.TextBox("Amount", null, new { @class = "inpshort" })</dd>
<dt>@Html.Label("Mededeling: ")</dt>
<dd>@Html.TextBox("Message", null, new { @class = "inplong" })</dd>
<dd class="buttons">
<input type="submit" value="Bevestigen" />
<input type="submit" value="Annuleren" />
</dd>
}
</dl>
</div>
这是控制器代码:
public ActionResult Transfer(int? id) {
Holder h = Holder.GetHolderByHolderId(Convert.ToInt32(User.Identity.Name));
List<Account> holderAccounts = Account.GetAccountsByHolderId(h.Id);
List<SelectListItem> ddlHolderAccounts = new List<SelectListItem>();
ddlHolderAccounts.Add(new SelectListItem { Text = "selecteer...", Value = "-1", Selected = true });
foreach (Account acc in holderAccounts) {
if (acc.Id == id) {
ddlHolderAccounts.Add(new SelectListItem { Text = acc.Name, Value = acc.Id.ToString(), Selected = true });
} else {
ddlHolderAccounts.Add(new SelectListItem { Text = acc.Name, Value = acc.Id.ToString() });
}
}
ViewData["ddlAccounts"] = ddlHolderAccounts;
return View();
}
[HttpPost]
public ActionResult Transfer(Transaction tra) {
if (ModelState.IsValid) {
Transaction.InsertTransaction(tra.Amount, tra.Message, tra.FromAccountId, tra.ToAccountNumber);
}
return View(tra);
}
现在我用谷歌搜索了很多,使用 DropDownListFor 填充你的下拉列表可能会更好?但是谁能给我举个例子?