0

There is a big chunk of html + js code. The goal is to load and show it only on a specific button click, so, there is no need in pre-loading it every time.

Normally, we can include a partial view dynamically by using jquery/ajax, but as far as I know it requires a controller. In this scenario, there is no need for a controller, as the data is static.

So, the question is: is it possible on a specific event (like button click) to include a partial view without going through a controller?

What we tried was giving the ajax request an address of partial view. However it didn't help.

<div class="reportWindow"></div>

<script type="text/javascript">
    $("#feedback").click(function () {

        $.ajax({
            url: "@Url.Content("_Feedback")",
            type: 'GET',
            cache: false,
            success: function (result) {
                $("#reportWindow").html(result);
            }
        });
    });    
</script>
3
  • 1
    You can load a static .html document without needing a controller. Commented Jan 22, 2013 at 14:19
  • qbantek, how can I make it upon button click? @Html.RenderPartial doesn't fit, as it will render when page is loaded. Commented Jan 22, 2013 at 14:25
  • @Bassam already answered how to do it (sorry I was in a hurry that is why i left a comment for others to extend) Commented Jan 23, 2013 at 15:10

1 Answer 1

2

a Partial view that have cshtml extension is still a Razor view, even if the content inside of it is static, the server still needs to read it and convert its content to html (even if that means copy it exactly the way it is and send it back)

What you need is an html file, which in turn you can load dynamically with javascript or jQuery:

$("#feedback").click(function () {
    $("#reportWindow").load('feedback.html'); 
});

Note that this will still ask the server for feedback.html, typically this won't match to any route then IIS will resolve the request by looking for a static pages called feedback.html!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.