Pass Value from View to Controller

Go To StackoverFlow.com

1

I'm looking to pass the value selected in the drop box when I click submit. Instead, it's passing a null value and I'm confused as to why. Any help is appreciated!!

Controller:

Namespace DHR
  Public Class WorkSampleController
        Inherits System.Web.Mvc.Controller
        Private dbQ As QuarterDBContext = New QuarterDBContext
        Private db As WorkSampleDBContext = New WorkSampleDBContext

Function Index() As ViewResult
            Dim quarterList As New List(Of String)()
            Dim QuarterQry = From d In dbQ.getQuarter
                             Order By d.ID
                             Select d.Quarter
            quarterList.AddRange(QuarterQry.Distinct())
            ViewBag.Quarter = New SelectList(quarterList)   
            Return View(db.WorkSample.ToList())
  End Function 
  <HttpPost()>
            Function Index(ByVal quarterID As Integer) As ActionResult
                Dim quarter As String = "null"
                If quarterID = 1 Then
                    quarter = "Dec, Jan, Feb"
                End If
                ViewBag.test = quarter
                Return View()
            End Function

View:

@ModelType IEnumerable(Of DHR.WorkSample)

@Code
    ViewData("Title") = "Monitor"
    Using (Html.BeginForm())
        @<p> Type: @Html.DropDownList("SampleType")
             Quarter: @Html.DropDownList("Quarter")
             <input type="submit" value="Filter" /></p>
    End Using
End Code
2012-04-05 20:32
by evglynn
**This made it work, we didn't use the correct passing of the values.
Using (Html.BeginForm("Post","ViewFolder",FormMethod.get) - evglynn 2012-04-09 19:15


0

Are you sure your code does really pass inside your post method? Because when I began asp.net (1 month ago), I had a problem with it.

I solved this by replacing (Html.BeginForm()) by (Html.BeginForm(IsPost)).

I'm not sure whether it will do the trick or not.

edit :

I've also already used this :

 Dim test1 As String = request("SampleType")
 Dim test2 As String = request("Quarter")
2012-04-05 23:28
by Deblaton Jean-Philippe


1

Im not sure, but if you do it in C# you could use the FormCollection as an inparameter to the ActionResult and get the value from there. this is how I would do it

public ActionResult Index(FormCollection coll){
      var value1 = coll["SampleType"];
      var value1 = coll["Quarter"];

}

Im sorry if it not works in VB but mabye its worth a shot?

2012-04-06 09:30
by mattematico
Ads