2009-04-08 40 views
20

我正在使用asp.net MVC框架。在我的頁面上我有一個dropdwonbox,當一個選項被點擊時,我想轉到另一個頁面。但我無法找到/如何將autopostback屬性設置爲true。這是我使用的代碼:C#如何設置使用asp.net mvc時的autopostback屬性?

.aspx的:

<%= 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(); 
} 

我有什麼做的,使用自動回功能?

回答

36

可以使用的onchange客戶端事件:

<%= Html.DropDownList("qchap", 
     new SelectList((IEnumerable)ViewData["qchap"], "Id", "Title"), 
     new { onchange = "this.form.submit();" }) %> 
+0

thnx。如果我想添加類屬性,我是否也必須使用相同的方式? – Martijn 2009-04-08 17:05:56

0

看來DropDownList的輔助方法,不支持這一點。 也許在表單和自定義的html屬性中使用它來提交表單。

0

我也相信,你可能要回傳調整到formsCollection

回傳公衆的ActionResult指數(FormsCollection myForm的)

(我不是在安裝了MVC我的家用電腦,所以我無法驗證此處的語法)

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> 

我希望它能幫助。我想你想要更完整的代碼,請感覺不錯,給我發電子郵件[email protected]

相關問題