0

I need to get the selected value of a dropdownlist when I click an ActionLink.

See The ActionLink and DropDownLists

Here is the dropdownlist I am binding from controller.

 @Html.DropDownList("resource", new SelectList(ViewBag.ResourceList, ViewBag.SelectedResource), "Resource", new { @class = "span6", @style = "width:14%; color:black;"})

And ActionLink with the function without [HttpPost]

@Html.ActionLink("Export Data", "ExportData");

I have tried from Request.Form["resource"] but it gives me null all the time

public ActionResult ExportData()
        {
            var req = Request.Form["resource"];
        }

I just need to get the text value whatever is in the DropDownList inside my ExportData Function.

Zubair
  • 27
  • 2
  • 6
  • 2
    DropDownList and Action link should be part of a form and Action link should be submitting the form in order for this to work. – Chetan Nov 09 '17 at 13:17
  • `ActionLink` is just a link, it doesn't post any values from any form. It sounds like you meant to create a submit button instead. – David Nov 09 '17 at 13:19
  • What if I use anchor tag? – Zubair Nov 09 '17 at 13:23
  • @Zubair: You *are* using an anchor tag. So I suspect the behavior would be the same if you continue to use an anchor tag. – David Nov 09 '17 at 13:25

2 Answers2

1

The action link basically renders an a tag, and the a tag will look something roughly like this;

<a href="ExportData">Export Data</a>

Because links issue a GET request, any parameters need to be passed via:

<a href="ExportData?resource=xyz">Export Data</a>

Request.Form will always be empty in a get request because a POST request populates the form collection. But either way, with a GET or POST request, you can pass the data as a parameter of the action method like:

public ActionResult ExportData(string resource)

So either put a <form> around the data you want to post to the server and change the hyperlink to a button to initiate the post, or use javascript to append "?resource=VAL" to the end of the hyperlink HREF attribute, where VAL is the value from the dropdown.

EDIT: in the few scenarios I had to do this before, what I'll normally do is on the link, add a data attribute (in C# API, use data_ for data attributes):

@Html.ActionLink("Export Data", "ExportData", new { data_url = "ExportData" })

The reason why I use a data attribute is to preserve the original URL and that way I don't have to do string manipulation. Using jQuery, on resource value change, you can update the URL easily:

$("#resource").on("change", function(e) {
  var val = $(this).val();
  var href = $("#linkid").data("url") + "?resource=" + val;
  $("#linkid").attr("href", href);
});

Anytime the dropdown changes, it updates the link url.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
-1

You should try the GetValues() method:

public ActionResult ExportData()
{
    var req = Request.Form.GetValues("resource");
}
red9350
  • 338
  • 3
  • 11
  • The `GetValues` method generally can't read values which aren't sent to the server in the first place. – David Nov 09 '17 at 14:43