Question

How to call dropdownlist selected value as @html.action route value. I am using following code in my MVC3 razor. I have to dynamically call 'eventid' as dropdownlist selected value.

@Html.Action("FirmPassForum", "Conference", new { eventId = 69 })
Was it helpful?

Solution

You can't do that, because the Html.Action helper renders at the server, whereas the dropdown selection could change on the client. One possibility is to use an AJAX call. So basically you could subscribe to the .change() event of the dropdown and send an AJAX call to some controller action that will return a partial view and update the DOM.

Start by placing this in a container:

<div id="container">
    @Html.Action("FirmPassForum", "Conference", new { eventId = 69 })
</div>

and then:

<script type="text/javascript">
    $(function() {
        $('#id-of-your-dropdown').change(function() {
            var eventId = $(this).val();
            $.ajax({
                url: '@Url.Action("FirmPassForum", "Conference")',
                type: 'GET',
                data: { eventId: eventId },
                cache: false,
                success: function(result) {
                    $('#container').html(result);
                }
            });
        });
    });
</script>

Fr this to work your FirmPassForum action should not be decorated with the [ChildActionOnly] attribute:

public ActionResult FirmPassForum(int eventId)
{
    MyViewModel model = ...
    return PartialView(model);
}

OTHER TIPS

You can use this also

`//var url = '@Url.Action("ActionName", "Controller")';

$.post("/Controller/ActionName?para1=" + data, function (result) {
    $("#" + data).html(result);
    ............. Your code
});`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top