Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Return Value From DropDownList in MVC using VB

Posted on 2014-02-10
9
Medium Priority
?
1,653 Views
Last Modified: 2014-02-16
My partner on a project set up a drop down list that pulls our data from the SQL database to display the content of the drop down. Now the issue is using that data. I have no idea where the data is being returned or posted, tried a million things, just need some help.

Here is the code in the controller for the drop down list
 Function Index() As ActionResult
            Dim List As New List(Of SelectListItem)
            List.Add(New SelectListItem With {.Text = "Select a Year", .Value = 0}) ' Adds first value as "Select a Year"
            Dim valQ = From y In db.gasrevperyears _
                       Select y.year, y.ID _
                       Order By year

            For Each k In valQ
                List.Add(New SelectListItem With {.Text = k.year, .Value = k.ID})
            Next

            ViewData("startDD") = List
            ViewData("endDD") = List

            Return View()

        End Function

Open in new window


Here is the function in the same controller for the charting function that will pass the correct data to the view for the chart.

<AcceptVerbs(HttpVerbs.Post)>
        Function Chart() As ActionResult
            'This is where the code to build the chart should be
            Dim startDD As String
            Dim endDD As String

            Dim List As New List(Of CChartData)
            Dim valQ = From y In db.gasrevperyears
                        Where y.year >= startDD AndAlso endDD <= 2010
                        Select y.year, y.amount
                        Order By year

            For Each k In valQ
                List.Add(New CChartData With {.Year = k.year, .ValueS1 = k.amount})
            Next


            Return View(List)
        End Function

Open in new window


Here is the implementation of the drop down lists in our view

@ModelType gasrevperyear

@Code
    ViewData("Title") = "Gasoline Revenue Per Year"
End Code

<div class="row-fluid">
   <div class="span12">
       <h2>Gasoline Revenue Per Year</h2>
   </div>
   @Using Html.BeginForm("Chart", "usergasrevperyear", method:= FormMethod.Post)
        @Html.ValidationSummary(True)
   @<fieldset>
        <legend>Please Select Graph Parameters</legend>

   <div class="row-fluid">
       <div class="span6">
       </div>
   </div>
   <div class="row-fluid">
       <div class="span6">
            <p>From Year</p>
                <div class="span6">
                    @Html.DropDownListFor(Function(model) model.year, ViewData("startDD"))
                </div>
        </div>
    </div>
    <div class="row-fluid">
        <div class="span6">
            <p>To Year</p>
            <div class="span6">
                @Html.DropDownListFor(Function(model) model.year, ViewData("endDD"))
            </div>
        </div>
    </div>
    <div class="span2 offset3">
        <button id="submit" name="submit" class="btn-btn-primary">Submit</button>
    </div>
    </fieldset>
    End Using
</div>

Open in new window

0
Comment
Question by:shanker702
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39850590
I see that you are using the Chart action method, on the usergasrevperyearController, according to this code:

Using Html.BeginForm("Chart", "usergasrevperyear", method:= FormMethod.Post)

Open in new window


You would need a parameter for the Chart method to accept a value from the submit and post-back.
0
 

Author Comment

by:shanker702
ID: 39850647
so would I just add the 2 parameters to the function? How do I use those 2 values in the function?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 39852153
When you use a POST to a method, the Razor engine tries to match up form values to the parameters, as described in this article:

6 Tips for ASP.NET MVC Model Binding
http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

<AcceptVerbs(HttpVerbs.Post)> _
Public Function Create(newRecipe As Recipe) As ActionResult
	' ...

	Return View()
End Function

Open in new window


Since you are using a model with the DropDownList, the engine will generate arguments that can be bound to that model type as a parameter to the Post method (Recipe in the example above).
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:shanker702
ID: 39855023
So would I try using the FormCollection parameter? and that will allow me to take what i need from the form once its submitted?
0
 

Author Comment

by:shanker702
ID: 39855067
Ok, so I created a class for the data, and now my chart function looks like:
Function Chart(chartData As DropDownData) As ActionResult
            'This is where the code to build the chart should be

            Dim startDD As Integer
            Dim endDD As Integer

            startDD = chartData.startDD

            endDD = chartData.endDD



            Dim List As New List(Of CChartData)
            Dim valQ = From y In db.gasrevperyears
                        Where y.year >= startDD AndAlso endDD <= 2010
                        Select y.year, y.amount
                        Order By year

            For Each k In valQ
                List.Add(New CChartData With {.Year = k.year, .ValueS1 = k.amount})
            Next


            Return View(List)
        End Function

Open in new window


So how do i render the drop downs in my form so that their values bind to the properties of the new object being created as a parameter
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39855079
If you are talking about this drop down:

 @Html.DropDownListFor(Function(model) model.year, ViewData("endDD"))

then, the argument should be the same type as the model.  Then, it will assign the values from the post-back to that argument.

You don't have to do anything else.
0
 

Author Comment

by:shanker702
ID: 39855247
So I changed it to this  
 <AcceptVerbs(HttpVerbs.Post)>
        Function Chart(startDD As gasrevperyear, endDD As gasrevperyear) As ActionResult
            'This is where the code to build the chart should be

            Dim startDD1 As String
            Dim endDD1 As String

            startDD1 = startDD.year
            endDD1 = endDD.year

Open in new window


But it still isn't getting anything from the post data from the drop down you referenced, and that's the same model type now.
0
 

Author Comment

by:shanker702
ID: 39855389
I finally figured it out thanks to you. Thanks a ton for pointing me in the right direction, I was looking at it all wrong
0
 

Author Closing Comment

by:shanker702
ID: 39855390
I finally figured it out thanks to you. Thanks a ton for pointing me in the right direction, I was looking at it all wrong
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question