将值从视图传递到控制器

时间:2012-04-05 20:32:11

标签: asp.net-mvc vb.net

我想点击提交时传递下拉框中选中的值。相反,它传递一个空值,我很困惑为什么。任何帮助表示赞赏!!

控制器:

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

查看:

@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

2 个答案:

答案 0 :(得分:1)

我不确定,但是如果你在C#中这样做,你可以使用FormCollection作为ActionResult的参数,并从那里获取值。我就是这样做的

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

}

对不起,如果它在VB中不起作用,但mabye值得一试?

答案 1 :(得分:0)

你确定你的代码确实在你的post方法中传递了吗?因为当我开始使用asp.net(1个月前)时,我遇到了问题。

我通过(Html.BeginForm(IsPost))替换(Html.BeginForm())解决了这个问题。

我不确定它是否会起作用。

编辑:

我也已经用过这个:

 Dim test1 As String = request("SampleType")
 Dim test2 As String = request("Quarter")