Jose Jose - 6 months ago 9
Javascript Question

RenderBody not rendering my views

Hi Im working on a site with a boostrap template. What i want is that when the user click a link, it would call a controller function return a view and focus to the body section.

To do that I have _Layout with my render body function

<section id="bodySection">
@RenderSection("scripts", required: false)

And a href that calls a funciton and focus on the body

<a href="#bodySection" onclick="myFunction()">Education</a>

my function the fires on the onclick:

<script type="text/javascript">
function myFunction() {
//alert("I am an alert box!");
$.post("@Url.Action("Education", "Education")");

And my controler

public ActionResult Education()
return View();

When I press the button the focus go to the body section, the controller is called but the body section remains with the old view. If I inspect the page i dont get any error.
Can somebody tell me what is going on with my code please??


Your code will not work because the tag is just an anchor link, it only scrolls the page to the point where #bodySection is and executes a $.post to a service that returns a certain result, but doesn't redirect to the new page.

I would change your link to:

<a href="@Url.Action("Education", "Education")">Education</a>

Then add the following script to the view in order to make the page automatically smooth-scroll to the anchor point every time on load:

<script type="text/javascript">
        scrollTop: $("#bodySection").offset().top
    }, 1000);