简体   繁体   中英

Action method in Controller is called eternally

I am having trouble with my site that I am creating. Trying to debug it, I noticed that when I hit F5 to run the site, it keeps creating the website on top of the one created earler, and keeps calling the action methods over and over again, and eventually firefox ends up eating all the CPU. I have noticed that it is this code:

Layout = "~/Views/Shared/_Layout.cshtml";

that causes it. If I remove this code from the View, it works fine. Any idea what might be the problem?

My view looks like this:

   @model WebApplication1.Models.IndexViewModel

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="pageContentDiv">
    <h2>@ViewBag.Title</h2>
    <p>Lorem</p>
</div>

My Layout looks like this:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>

    <link href="~/Scripts/jquery-ui.min.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-ui.min.js"></script>
    <script src="~/Scripts/OwnedScripts/InitializeUI.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            initUI();
        });        
    </script>
</head>
<body>
    <div class="topMenu">
       <div id="topMenuContentDiv" style="margin-left: 110px;">
           <ul>
               <li>@Html.ActionLink("Home", "Index", "Home")</li>
               <li>@Html.ActionLink("Roads", "Recs", "Home")</li>
               <li>@Html.ActionLink("Software", "Software", "Home")</li>
               <li>@Html.ActionLink("About", "About", "Home")</li>
               <li>@Html.ActionLink("Contact", "Contact", "Home")</li> 
           </ul>
       </div>
    </div>

    <div class="contentDiv" id="#contentDiv">
        <div id="decor" style="height: 640px; float: right;">
            <div id="decorPNG" style="background-image: url('../Images/VisualImages/decor.png'); width: 300px; height: 64px;
                float: right; margin-right: -20px; margin-top: 100px;">
            </div>
        </div>

        <div id="logoDiv">
            <h1>QitozNet</h1>
        </div>

        @RenderBody()

      <footer class="footer">
          <p>&copy; @DateTime.Now.Year - All rights reserved</p>
          <hr />
      </footer>        
    </div>
</body>
</html>

And Controller looks like this:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

       public ActionResult Recs()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Temp";

            return View();
        }

        public ActionResult Software()
        {
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }        

    }
}

The Index method is getting called over and over again, returning in an eternal loop it seems. When debugging it, I kept pressing F10 (step over) for 5 min, and realized that it was just creating the page over again, but on top of the older one, so it ends up duplicating the site

I have also tried to remove the: Layout = "~/Views/Shared/_Layout.cshtml"; from the Views, so that they only reside in the: _ViewStart.cshtml but no luck

The initGUI script looks like this:

    function initUI() {
    $(".topMenu").tabs();
    $("#logoDiv").toggle("slide", { direction: 'left' }, 2000);

    $("#decorPNG").mouseover(function () {
        $(this).css("cursor", "pointer");
        $(this).draggable({ axis: 'y', containment: "#decor" });
    });
}

Your issue is here:

<div class="topMenu">
   <div id="topMenuContentDiv" style="margin-left: 110px;">
       <ul>
           <li>@Html.ActionLink("Home", "Index", "Home")</li>
           <li>@Html.ActionLink("Roads", "Recs", "Home")</li>
           <li>@Html.ActionLink("Software", "Software", "Home")</li>
           <li>@Html.ActionLink("About", "About", "Home")</li>
           <li>@Html.ActionLink("Contact", "Contact", "Home")</li> 
       </ul>
   </div>
</div>

When you use jQueryUI tab, it uses the anchor tag to load the content into that tab, and then display the content when you click on it. Your issue is that the Home controller Index action is in your menu and the default item so you have a recursive loop.

If you really want to use jQueryUI tabs, then you should disable the href ActionLink for the current page. Or you should render PartialViews for your ActionLinks and use your Home/index as the container. Then your app would function more like a SPA (single page application).

Here are 2 possible solutions depending on what your aim is: One way to achieve what I think you're looking for using jQuery UI. Copy your Index action and View to another name like "Default". Then change that action and all the other actions to return PartialViews.

public PartialViewResult Default()
{
    // do stuff
    return PartialView();
}

Then remove any code in your Index action that you don't want to display on all pages. This now will be your container, and then your tabs will render the partial views of all of those pages. Your PartialView will render the same exact code as before, but without the _Layout.cs file. These will still use the ActionLink methods in your .cshtml files.

A second approach is to use Bootstrap Tabs . That is the CSS approach, and you can add the css class "active" based on the active page, without making any changes to your ActionResults.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM