views:

64

answers:

2

I have a drop down where I want to pass the selected value I selected to the controller. How do I accomplish this?

+1  A: 

You can pass via the querystring like so:

http://YourMvcApp/YourController/YourAction/?yourvariable=yourvalue

Then inside your Action you can get to the value by doing

Dim myValue As Object = Request("yourvariable")

Or in C#

Object myValue = Request["yourvariable"];

HTH,

Mike

Mike C
+2  A: 

Controller:

public class TestController : Controller
{
    public ActionResult Test(Test input)
    {
        string selected = input.YourDropDown; //Here is your value

        return View();
    }

}

Model:

public class Test
{
    public string YourDropDown { get; set; }
}

View:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<###NAMESPACE###.Models.Test>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test</title>
</head>
<body>
    <div>
        <% using (Html.BeginForm()) { %>
            <%= Html.DropDownListFor(model => model.YourDropDown, new[] { new SelectListItem { Text = "Test", Value = "Value" }}) %>
            <input type="submit" />
        <% } %>
    </div>
</body>
</html>

Put the view in the folder like: /Views/Test/Test.aspx

and your url will be:

url/Test/Test

This would properly be one of the better and most extensible ways to do what you what. You could avoid to make the model class and use Html.DropDownList instead of Html.DropDownListFor.

lasseespeholt