views:

463

answers:

4

Hi all,

I have a drop down in a MVC View, which is some thing like this:

Html.DropDownList(id, Range(0,10)
                        .Select(x => new SelectListItem {Text = x, Value = x}))

In the view itself, I need the selected value of this drop down. I know that I can access that in a JavaScript, but I am looking for a way to get it in the view itself from the drop down properties or something like that.

How can I access it? I tried to figure out some thing from intellisense but nothing relavant showed up, help is much appreciated.

Edit: I want the value after a few lines after the declaration of the drop down, I know that I can access it from JavaScript and by posting the form, Is there noway to access it on the view itself ?

Edit2: If its not possible to access it in view, please explain the reason, I am more interested in knowing it.

Thanks.

+1  A: 

To get the selected value you could use either javascript or a controller action to which the form containing the select box is submitted.

Darin Dimitrov
I want it in the view itself, please see the edit. Thanks
Mahesh Velaga
The View has no way of knowing the selected value if the controller didn't pass it in the model or `ViewData`. So you first post the form to a controller action which gets the selected value and then it uses this value to pass it back to the view.
Darin Dimitrov
actually thats what I wanted to know, why does the view not know about the selected value? could you please explain? Thanks
Mahesh Velaga
A: 

The selected value will be in the FormCollection or QueryString collection with the name of the DropDown's ID in the controller action that receives the form submission from this view. You can either submit the values with a classic POST or GET or via AJAX, but you have to wire up a server-side action that processes that input.

Codewerks
I want it in the view itself, please see the edit. Thanks
Mahesh Velaga
A: 

There is a SelectList class as well wich allows you to define wich item will be selected.. and knowing that - you will know selected value.. Also.. if no value is selected explicitly, dropdowns tend to select the first item in the list.

Alexander Taran
+1  A: 

After reading at your question, it sounds like you want to have the drop down list supply a value for a lower section of the same page.

First and foremost, you will need to place the DropDownList within a form construct, as in:

<% using (Html.BeginForm("ProcessValue", "ThisPage")) { %>
    <%= Html.DropDownList("DropID", Range(0, 10).Select(a=>new SelectListItem { Text = x, Value = x }) %>
    <input type=submit value="Submit" />
<% } %>

You need to set up a few things ahead of time:

  1. You have to have a submit button, or a similar construct, in order to retrieve the value of the DropID variable.
  2. You need to set up an controller method that will handle the processing of the value, then redirect back to the original page with the selected value in the page's ViewData.
  3. Finally, you need to set up the view so that the post-processing section will only display if you have a valid value in the DropID variable.

It's not as simple as just placing a DropDownList in a view and then using that value later on in the page. You have to get the controller involved to manage the data transport and you have to set up the single view to handle multiple states (ie. before the value is selected and after the selection takes place).

Neil T.
The View component of the Model-View-Controller serves only to display data received from the controller. It's not intended to have any knowledge of the purpose of the data or to assign meaning to the data. It's not supposed to know whether an <option> is selected or not. The view can identify those data elements that need to be sent to a controller for further processing, but only if those data elements are within a <form>. If you want "smart server controls", you're better off doing what you need to do in ASP.NET WebForms, not MVC.
Neil T.