Get DropDownListFor selected value into Ajax.ActionLink call

时光毁灭记忆、已成空白 提交于 2019-12-11 08:58:38

问题


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.


回答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.



来源:https://stackoverflow.com/questions/20425558/get-dropdownlistfor-selected-value-into-ajax-actionlink-call

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