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

4

21

I am using asp.net MVC framework. On my page i have a dropdwonbox and when an option is clicked i want to go to another page. But i can't find how/where to set the autopostback property to true. This is the code i'm using:

Aspx:

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

Controller:

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

    return View();
}

What do i have to do to use the autopostback functionality?

Gelatinate answered 8/4, 2009 at 16:30 Comment(0)
L
37

You can use the onchange client event:

<%= Html.DropDownList("qchap", 
       new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" ),
       new { onchange = "this.form.submit();" }) %>
Laura answered 8/4, 2009 at 16:40 Comment(3)
thnx. And do i have to use this same manner if i want to add the class attribute?Gelatinate
yes, although with c# you'll need to prefix with an underscore.. ie new { _class = "something" }Sombrous
How the controller will know that which action needs to be executed?Fico
B
0

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.

Biegel answered 8/4, 2009 at 16:41 Comment(0)
T
0

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)

Turman answered 9/4, 2009 at 12:15 Comment(0)
A
0

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>
Age answered 9/1, 2010 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.