0

因此,我想获取下拉列表的值(== 帐户 ID)以插入新交易,您可以在下面找到我的视图:

    <div id="content">
        <h2>Overschrijving</h2>
        <p id="topmsg">velden met een * zijn verplicht</p><p>&nbsp;</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 填充你的下拉列表可能会更好?但是谁能给我举个例子?

4

2 回答 2

1

通过查看您的代码,我可以看到您没有将 SelectListItems 列表传递给 DropDownList 助手。您可以通过以下两种方式之一执行此操作。

1-将其绑定到模型上的属性:

@Html.DropDownListFor(x=>Model.Property, new List<SelectListItem> { new SelectListItem { Text = "Item1", Value = "Value1" })

或者

2-您可以在不绑定到模型属性的情况下执行此操作,例如:

                @Html.DropDownList("propertyName", new List<SelectListItem> { new SelectListItem { Text = "Item1", Value = "Value1" } })

如果您使用第二种方法,那么您的控制器操作在提交时必须接受“propertyName”作为参数。

And don't forget to provide a list of SelectListItems to select from (which you're not doing in your code).

Hope this helps.

于 2012-11-26T00:13:19.310 回答
0

should be @Html.DropDownListFor(x=>Model.Property, new List { new SelectListItem { Text = "Item1", Value = "Value1" }})

于 2013-07-24T16:58:08.503 回答