1

How do I get the DropDownListFor selected value into the ActionLink route value (OrderId)?

@Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, "OrderId", "Total"))

@Ajax.ActionLink("View Order", "OrderDetails", 
    new 
    { 
        OrderId = 1 // Dropdown value here!
    },
    new AjaxOptions() 
    {
        HttpMethod = "GET",
        UpdateTargetId = "OrderDetailsDiv",
        InsertionMode = InsertionMode.Replace
    })

<div id="OrderDetailsDiv"></div>

I am using MVC 5.

Dave New
  • 38,496
  • 59
  • 215
  • 394

1 Answers1

1

Could you put your dropdown list and link in an AjaxForm?

@using (Ajax.BeginForm("OrderDetails", new AjaxOptions { UpdateTargetId = "OrderDetailsDiv" }))
{ 
    @Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, "OrderId", "Total"))
    <input type="submit" value="View Order" />
}

You would have a controller method named OrderDetails, which takes a model object, containing a property with name CustomerId.

See this answer for more information on AjaxForms and models: Using Ajax.BeginForm with ASP.NET MVC 3 Razor

For more information: http://geekswithblogs.net/blachniet/archive/2011/08/03/updating-partial-views-with-unobtrusive-ajax-in-mvc-3.aspx

Otherwise, I would do a post using JavaScript/jQuery, where you can retrieve the dropdown value, before making the request.

Community
  • 1
  • 1
cederlof
  • 7,206
  • 4
  • 45
  • 62
  • Thanks for the answer. Even with the AjaxForm, how do I get the `DropDownListFor` selected value? – Dave New Dec 06 '13 at 14:01
  • Refer to this answer: http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor – cederlof Dec 06 '13 at 14:04