8bitboy
8bitboy

Reputation: 63

ASP.NET MVC4 + Get drop down value after HttpPost submit

So I want to get a value of the dropdown (== id of the account) to insert a new transaction Here below you can find my View:

    <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>

this is the Controller code:

        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);
        }

Now I searched a lot with Google, it's probably better to use the DropDownListFor to fill your drop down list? But could anyone show me an example?

Upvotes: 0

Views: 2550

Answers (2)

Texellab
Texellab

Reputation: 101

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

Upvotes: 0

Mark
Mark

Reputation: 1748

By looking at your code, I can see that you're not passing a list of SelectListItems to the DropDownList helper. You can do this one of two ways.

1- Bind it to a property on your model:

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

Or

2- You can do it without binding to a model property like:

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

If you're using the second approach then your controller action must accept "propertyName" as a parameter when submitting.

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

Hope this helps.

Upvotes: 1

Related Questions