21

我正在使用 asp.net MVC 框架。在我的页面上,我有一个 dropdwonbox,当单击一个选项时,我想转到另一个页面。但我找不到如何/在哪里将 autopostback 属性设置为 true。这是我正在使用的代码:

ASP:

<%= Html.DropDownList("qchap", new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" )) %>

控制器:

public ActionResult Index(int id)
{
    Chapter c =  new Chapter();
    ViewData["qchap"] = c.GetAllChaptersByManual(id);

    return View();
}

我必须做什么才能使用自动回发功能?

4

4 回答 4

37

您可以使用 onchange 客户端事件:

<%= Html.DropDownList("qchap", 
       new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" ),
       new { onchange = "this.form.submit();" }) %>
于 2009-04-08T16:40:17.477 回答
0

似乎 DropDownList 辅助方法不支持这一点。也许在表单中使用它并使用自定义自定义 html 属性来提交表单。

于 2009-04-08T16:41:40.377 回答
0

我也相信您可能希望将回发调整为 formsCollection

回发公共 ActionResult 索引(FormsCollection myform)

(我不在安装 MVC 的家用电脑上,因此无法验证此处的语法)

于 2009-04-09T12:15:09.040 回答
0

我使用此代码解决。

Function Index(ByVal collectionField As FormCollection) As ActionResult

        Dim industryCategoryID As Long = collectionField.Item("ddlIndustry")
        If industryCategoryID = 0 Then
            Me.ViewData("IndustryList") = GlobalController.GetIndustryList
            Return View(_service.ListCompanies())
        Else
            Me.ViewData("IndustryList") = GlobalController.GetIndustryList
            Return View(_service.ListCompanies(industryCategoryID))
        End If

End Function

这是针对 ActionResult 功能的

然后对于视图

 <p>
     <% Using Html.BeginForm()%>
        <%=Html.DropDownList("ddlIndustry", New SelectList(CType(ViewData("IndustryList"), IEnumerable), "ID", "Name"), "--Choose industry--", New With {.onchange = "this.form.submit()"})%>
     <% End Using %>  

    </p>

我希望它有所帮助。如果您想要更完整的代码,请发送电子邮件至boylevantz@gmail.com。

于 2010-01-09T13:44:45.700 回答