views:

57

answers:

3

I have a dropdown list on my master page that needs to postback after being changed. After the postback, whatever page initiated the postback needs to be re-displayed.

My question is where do I handle this? Obviously I do not want to have to modify every Action in my project... My guess is to maybe postback to some other fixed action and have that action redirect back to the page that is the referrer. Am I correct? Any thoughts?

Thanks.

Jason

A: 

When you post back from the dropdown list change what are you doing? Can you maybe handle this in a jQuery call thus eliminating the need to re-display the page at all?

griegs
I use jQuery and ajax in other parts of the site but in this case it needs to be a postback. In essence the dropdown acts as a toggle between several different roles ("Admin", "User", "Developer") that changes the page view completely.
Jason
A: 

Calls to Action Methods can be asynchronous as griegs says, as such, you can post whatever information you need from the radio buttons to an action method without needing to reload the page.

If you need to update a part of the page, you can replace it with the contents of a rendered action method. If you use the jQuery ajax methods, you can post specific information to your methods.

For example, something like this:

$(document).ready(function()
{
   $("#myRadioButton").change(function()
    {
       //Post to your action method, with the value of the radio button, function to call on success
        $.post('yourActionMethodUrl', $(this).val(), function()
        {
           //Update some part of the page
        });
    });
});

This is based on memory, so you may need to check the syntax.

TreeUK
A: 

In Site.Master, I ended up wrapping the dropdown within its own form that posted back to a dedicated controller/action.

<% Using Html.BeginForm("ChangeRole", "Home")%>
   <div id="roleSelector">Change Role: <%=Html.DropDownList("Role", DirectCast(ViewData.Item("Roles"), SelectList), New With {.onchange = "this.form.submit();"})%></div>
<% End Using%>

In the controller I used the following code to change the mode and then redirected back to the referring URL.

<AcceptVerbs(HttpVerbs.Post)> _
Public Function ChangeRole() As ActionResult
    Me.CurrentUser.SetRole(DirectCast([Enum].Parse(GetType(Models.ApplicationRoles), Me.Request.Item("Role")), Models.ApplicationRoles))
    Return Redirect(Request.UrlReferrer.ToString())
End Function

I am unsure if this is the recommended way but I have been unable to think of another solution.

Jason