0

In my Index view I have this:

<script type="text/javascript" src="~/Scripts/jquery-2.0.0.js"></script>
<script type="text/javascript" src="~/Scripts/Javascript1.js"></script>

<div id="dictionary">
</div>

<div class="letter" id="letter-a">
<a href="#">A</a>
</div>

In my Javascrip11.js I have this:

$(document).ready(function () {
 $('#letter-a a').click(function () {
    $('#dictionary').load('HtmlPage1.html');
     alert('Loaded!');
        return false;
    });
});

And in my HtmlPage1 I have this:

<div>Definitions for letter A</div>

I want the Javascript to load this HtmlPage1 and insert into the div dictionary of the page when I click the anchor tag.....I have HtmlPage1 both in the Scripts folder and in the ViewFolder.. Both doesnt work.... I am getting the alert but the snipet is not being inserted....

0

5 Answers 5

1

When loading a static file in MVC are basically using an absolute path. Put the HtmlPage1.html file in the root of the site and everything should work

Of course you can use subfolders as well (or a controller-action, which sounds hacky though):

$('#dictionary').load('Views/HtmlPage1.html');
Sign up to request clarification or add additional context in comments.

Comments

1

You will have to make a "Ajax Controller" (or name of your choice) so you can load the page through a URL.

Sample:

Place the "HtmlPage1" in the "View/Ajax"-folder.

Create a controller named "AjaxController" with the following action:

public class AjaxController
{
     public ActionResult HtmlPage1()
     {
         return View();
     }
}

And then call the url "Ajax/HtmlPage1" in your JavaScript:

$('#dictionary').load('/Ajax/HtmlPage1');

From here you can add more ActionResults to add different results. :)

Comments

1

try this:

$(document).ready(function () {
  $('#letter-a a').on("click", function (e) {
    e.preventDefault();
    $('#dictionary').load('HtmlPage1.html');
    alert('Loaded!');
    return false;
  });
});

Comments

0

I think you need to preventDefault behaviour on <a> tag:

$(document).ready(function () {
 $('#letter-a a').click(function (e) {
    e.preventDefault();
    $('#dictionary').load('HtmlPage1.html');
     alert('Loaded!');
        return false;
    });
});

Comments

0

Try this :-

$(document).ready(function () {
 $('#letter-a a').click(function () {
     $.get('HtmlPage1.html')
     .success(function(data) {
       alert(data);
         $('#dictionary').html(data);
     });

        return false;
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.