JavaScript load PartialView passing a Model

1

I need to call a PartialView in my JavaScript but I'm not sure how.

I'm using a plugin that in the variable TARGET receives a Div. And in my case, this Div I put in being a Partial View. My JS looks 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 because I'm using the helper inside JavaScript. But the example is to convey my need.

  

So I need JavaScript to load a PartialView and still   pass a model to her. Is this possible?

    
asked by anonymous 23.03.2017 / 15:59

1 answer

1

This will not 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 View data to work, it must be mounted in 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 assuming there's this statement in your _Layout.cshtml :

@RenderSection("scripts", required: false)
    
23.03.2017 / 16:45