Javascript load Partialview passing a Model

Asked

Viewed 613 times

1

I need to call a Partialview in my Javascript but I’m not sure how.

I’m using a plugin that in the TARGET variable gets a Div. And this in my case, this Div I put being a Partial View. My JS is like this:

    var modal = new Custombox.modal({
        content: {
            effect: 'blur',
            target: '#MinhaDiv',
            }
        }).open();

The idea would be to do something close to that:

var modal = new Custombox.modal({
        content: {
            effect: 'blur',
            target: @Html.Partial("_Unidade", Model),
            }
        }).open();

Of course this example will not run pq I am using the helper within Javascript. But the example is to convey my need.

So I need Javascript to load a Partialview and still pass a model to her. This is possible?

  • Yes it is possible, but it should be so, when loading your page that contains this div already load the partial directly to it, and after you call via javascript the same was already ready to be used

1 answer

1


This isn’t gonna work:

var modal = new Custombox.modal({
    content: {
        effect: 'blur',
        target: @Html.Partial("_Unidade", Model),
        }
    }).open();

You need to call @Html.Partial before the Javascript declaration. If the Javascript declaration needs data from View to function, it must be mounted on View using @section Scripts {} at the end of View:

@Html.Partial("_Unidade", Model)

@section Scripts
{
    <script>
        var modal = new Custombox.modal({
            content: {
                effect: 'blur',
                target: '#MinhaDiv',
            }
        }).open();
    </script>
}

I’m guessing there’s this statement in your _Layout.cshtml:

@RenderSection("scripts", required: false)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.