tags:

views:

539

answers:

2

I'm trying to create an ActionLink in one of my views that sends the selected value of a dropdown list to a new action. So far I have this, I just need to find a way to populate the ID on the end of my ActionLink.

   <%= Html.DropDownList("StatusDropDown") %>
   <%= Html.ActionLink("Apply","Index",new {Controller="Tasks", Action="Index", id="DROPDOWN LIST SECLECTED VALUE"}) %>

Obviously this link would need to be updated whenever the selected index of the drop down is changed. Is this something I need to do in javascript or is there a better way of managing this from within ASP.Net MVC?

Thanks

A: 

A link goes to another page, it is in effect a redirect. The only way to update where that link goes to with reference to the drop down list is with javascript.

It sounds like you want a kind of submit action. In that case you should use a form and a submit button, creating the appropriate handlers in your controller. Remember you can just do a redirect in your controller based upon the submitted value of the form. So something like this:

<form method="post" action="/MyForm">
    <input type="select" name="mySelect">
        <option value="1">First Option</option>
        <option value="2">Second Option</option>
    </input>
</form>

And in your controller:

public ActionResult MyForm(int mySelect)
{
    return Redirect(String.Format("myurl?id={0}", mySelect));
    // Note the above is only preferable if you're going to an external link
    // Otherwise you should use the below:
    return RedirectToAction("myAction", new { id = mySelect });
}

Obviously in this simplified example, the MyForm proxy to your desired action is redundant, but it illustrates the idea so you can apply it to your specific situation.

joshcomley
I thought about this, too, but I dislike the two round-trips for a single action in the case where you want to keep your URLs RESTful.
tvanfosson
Yes, but this is just an example. You could just have the form submit to /myAction - I don't know his specific setup, but this was to illustrate the principle
joshcomley
The reason I was using an actionLink is because I tried using a form and it kept using querystrings for example Tasks/?StatusId=1 Where as I'm trying to get something like Tasks/filter/1/open Where 1 is the id of the status and open is the name, I'd like readable URLs for this part of the site
Gavin Draper
Well then use my approach and redirect to a custom URL you build yourself. Remember the ID is a special case that if on its own should be turned by asp.net mvc not into "/mypage/?id=1" but /mypage/1", either way my example above can do exactly what you want.
joshcomley
+1  A: 

If you don't want to use form submission (i.e., want the parameter passed as part of the url instead of a form parameter), you'll need to build the url client-side with javascript.

<%= Html.DropDownList("StatusDropDown") %>
<a id="applyLink" href="#">Apply</a>

<script type="text/javascript">

    function setHref( elem, val )
    {
        if (val) {
           $(elem).attr( "href", "/Tasks/" + val );
           $("#applyLink").unbind("click");
        }
        else {
           $(elem).attr( "href", "#" );
           $("#applyLink").click( function() { alert( "No value chosen" ); } );
        }
    }

    $(function() {
       var dropdown = $("#StatusDropDown");
       dropdown.change( function() {
           setHref( this, $(this).val() );
       });
       setHref( dropdown, null );
    });
</script>
tvanfosson