1

In my view my select tag is like this:

<select name="selectedItem" id="selecttag" onchange="GetSelectedItem()">
    <option value="select">Select any value</option>
    <option value="Objective">Objective</option>
    <option value="Subjective">Subjective</option>
</select>

I am using a stored procedure to pass data to a database. How can I pass my selected value to my controller?

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
minakshi
  • 315
  • 1
  • 3
  • 17

2 Answers2

0

You could use a view model:

public class MyViewModel
{
    public string Value { get; set; }
    public IEnumerable<SelectListItem> { get; set; }
}

then you could have a controller which is populating this model and passing it to the view:

public ActionResult Index()
{
    var model = new MyViewModel();
    // TODO: you could load the values from your database here
    model.Values = new[]
    {
        new SelectListItem { Value = "Objective", Text = "Objective" },
        new SelectListItem { Value = "Subjective", Text = "Subjective" },
    };
    return View(model);
}

and then have a corresponding strongly typed view in which you would use the Html.DropDownListFor helper:

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.DropDownListFor(x => x.Value, Model.Values, "Select any value");
    <button type="submit">OK</button>
}

and finally you could have a corresponding controller action to which the form will be submitted and taking the view model as parameter:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // model.Value will contain the selected value here
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0
[HttpPost]
 public ActionResult Index(MyViewModel model,string selectedItem) //"selectedItem" is the name of your drop down list.
 {
   //here by "selectedItem" variable you can get the selected value of dropdownlist
 }
jishnu saha
  • 175
  • 7