0

I have a dropdownlist in my View:

@Html.DropDownListFor(m => m.APtTitleData.apt, (IEnumerable<SelectListItem>)ViewBag.apt, "-Select Apt-", new { id = "SelectedAPt", name= "SelectedAPt" })

and I have a button in the same View

<a href="@Url.Action("Edit", "APtTitle", new { id = Model.APtTitleData.aPtId, SelectedAPt = "???" })">GO</a>

How do I pass the value of the dropdown to my controller (Edit)? I'm trying to get the value to the button, but I'm not sure this is the right way. Any other idea?

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
Waller
  • 433
  • 1
  • 6
  • 20
  • Please refer: https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc – Jimesh Dec 19 '20 at 06:12

1 Answers1

0

I would suggest that you use a form where you submit the value from the dropdown to the controller.

Please check out the following code:

@using (Html.BeginForm("APtTitle", "Edit", FormMethod.Post))
{
    <div class="form-group">
            <label>AptTitles</label>
            <div>
                @Html.DropDownListFor(m => m.APtTitleData.apt, (IEnumerable<SelectListItem>)ViewBag.apt, "-Select Apt-", new { id = "SelectedAPt", name= "SelectedAPt" })
            </div>
        </div>
        
        <div class="form-group">
            <div>
                <input type="submit" value="Submit" />
            </div>
        </div>
    </div>
}

And in the controller your code should look similar to the following:

[HttpPost]
public ActionResult Edit(string SelectedAPt)
{
   var aPtValue = SelectedAPt;

    // Do what intended with the value
}

You should now be able to see the value in the controller now. Try it out and let me know if you run into issues.

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
IceCode
  • 1,466
  • 13
  • 22