C# How to set the autopostback property when using asp.net mvc?

喜欢而已 提交于 2019-11-30 11:04:06

You can use the onchange client event:

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

It seems the DropDownList helper method doesn't support this. Maybe using it within a form and a custom custom html attribute to submit the form do it.

I believe too that you may want to adjust your postback to the formsCollection

postback public ActionResult Index(FormsCollection myform)

(I'm not on my home pc where MVC is installed, so I can't verify the syntax here)

I solve using this code.

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

That's for the ActionResult function

And Then for the View

 <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>

I hope it helps. I f you would like more complete codes please feel good to email me at boylevantz@gmail.com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!