0

I'm working with ASP.NET & MVC.

There's a view that has some fields and then I added an HTML select drop down menu. I want to be able to grab the value of the select item without including it in the corresponding model; is this possible? Ho could I reference the HTML on the page? would I use javascript? Not sure where to begin

tereško
  • 58,060
  • 25
  • 98
  • 150
fifamaniac04
  • 2,343
  • 12
  • 49
  • 72
  • What do you mean "I want to be able to grab the value of the select item without including it in the corresponding model"? Do you want to do something with it in your server side code? Do you want to just do something with it on the front end? – mituw16 Dec 12 '14 at 18:04
  • i want to do something with it on the server – fifamaniac04 Dec 12 '14 at 18:06
  • Do you want to access the selected value in the drop down after the user submits the form to the controller? Or are you trying to use javascript to access value before the user submits? – Slider345 Dec 12 '14 at 18:06
  • I want to access the selected value in the drop down after the user submits the form to the controller? – fifamaniac04 Dec 12 '14 at 18:06
  • @fifamaniac04 Then you will need to make a property in your view model for it. Relevant code would help us – mituw16 Dec 12 '14 at 18:06
  • @mituw16 would you recommend making the property a string then to hold the value of the selected item or just make it a DropDownlistData object? – fifamaniac04 Dec 12 '14 at 18:08
  • @fifamaniac04 see my answer for a ViewModel approach – mituw16 Dec 12 '14 at 18:14

2 Answers2

3

Once the form has been post to the server, you can access the any form values in the controller by useing in the Request object, like so:

[HttpPost]
public ActionResult Edit()
{
    var value = Request["MyDropDown"];

    /* process and return */
} 

assuming the Drop Down has a name attribute of "MyDropDown".

See this answer on SO.

Of course, using "View Models" to represent the data submitted on forms is a big part of MVC, so I would think over this approach and make sure it's the right way to do things.

Community
  • 1
  • 1
Slider345
  • 4,558
  • 7
  • 39
  • 47
  • simple, didn't need to modify the Model/View. does what I want... only thing that Request took the name of the Drop Down, not the ID – fifamaniac04 Dec 12 '14 at 18:20
0

Since this is dealing with MVC, I'll go ahead and present a different approach..

In your View Model you will need to add 2 items, 1 property to store the value of your select list, and another to actually house the items in your select list.

View Model

public class ViewModel 
{
    public String SelectListValue { get; set; }

    public List<SelectListItem> SelectListValues { get; set; }

    public ViewModel() 
    {
        SelectListValues = new List<SelectListItem>();
        SelectListValues.Add(new SelectListItem { Text = "Display Text" Value = "Some Value" });
    }
}

Then in your view

View

@Html.DropDownListFor(x => x.SelectListValue, SelectListValues, null)
mituw16
  • 5,126
  • 3
  • 23
  • 48