Вопрос

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 })
Это было полезно?

Решение

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);
}

Другие советы

You can use this also

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

$.post("/Controller/ActionName?para1=" + data, function (result) {
    $("#" + data).html(result);
    ............. Your code
});`
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top