The view 'Home' or its master was not found or no view engine supports the searched locations. The following locations were searched: 'sporadic error'

StackOverflow https://stackoverflow.com/questions/12736684

سؤال

I am getting sporadic errors when working with my MVC 4 application when trying to return a View.

In this particular case, I am about to return a View return View("Home", model); and that's where I get the msg. It also seems to sporadically happen when you're constantly testing & debugging and I think the View Engine goes bonkers. For instance, before this one, I was executing a simple view and said it couldn't find it when it was there all the time. After a combination of clearing cache, rebooting, etc, and executing the same exact logic, it worked.

So, I don't know how to fix this issue with the View Engine. Right before I come back into the View, I can assure you that I have records in my model. I can't attach a screen print on this form from my pc --- no option for it.

So, the dire question is: How do I solve this issue where it doesn't sporadically happen like this??? This is a serious problem and hope to get it fixed....

I looked at Scott Hanselmans nuget package for precompiling views, etc, but seems overcomplicated and extra work. I was wondering if there is something else I can do.

Any help would be much appreciated....

Here is my Globla.asax file:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

If someone can please answer, I would appreciate it as this would have our MVC apps that we are working on come to a halt!

I tried adding the .DataTokens.Add("area", "YOURAREANAME"); onto the end of the MapRoute, but I don't know what to substitute for the strings.

In addition, I don't know why this needs to be done (if it will fix it) and need an explanation from someone...

Added code for another person that wants to check out the controller code.

[HttpPost]
        public ActionResult Refresh(ViewModelTemplate_Guarantors model)
        {
            try
            {
                model.Error = string.Empty;

                bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(model.LoanId));

                if (!dbHasRows)
                {
                    ViewBag.ShowTemps = false;
                    model.Error = "Details not available for this LoanId.";
                    return View("Home", model);
                }
                else
                {
                    int TemplateCnt = 0;
                    int GuarantorCnt = 0;
                    ViewBag.ShowTemps = true;

                    ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(model.LoanId), "1", model.SelectedDeptText, out TemplateCnt, out GuarantorCnt);

                    if (TemplateCnt > 0)
                        model.Templates = tg.Templates;
                    else 
                       model.ErrorT = "Templates not available for this LoanType.";

                    if (GuarantorCnt > 0)
                        model.Guarantors = tg.Guarantors;
                    else
                        model.ErrorG = "Guarantors not available for this LoanId.";

                    return View("Home", model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

i DON'T UNDERSTAND WHY THE ROUTING engine tries to go to the following, when I my directory structure is clearly: Views/Home/Index.cshtml

The error that is givien is below and doesn't even look for the "Index" page..

~/Views/Home/Home.aspx
~/Views/Home/Home.ascx
~/Views/Shared/Home.aspx
~/Views/Shared/Home.ascx
~/Views/Home/Home.cshtml
~/Views/Home/Home.vbhtml
~/Views/Shared/Home.cshtml
~/Views/Shared/Home.vbhtml
هل كانت مفيدة؟

المحلول

Replace this:

return View("Home", model);

with this:

return View("Index", model);

Complete code:

public ActionResult Refresh(ViewModelTemplate_Guarantors model)
{
    try
    {
        model.Error = string.Empty;

        bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(model.LoanId));

        if (!dbHasRows)
        {
            ViewBag.ShowTemps = false;
            model.Error = "Details not available for this LoanId.";
            return View("Home", model);
        }
        else
        {
            int TemplateCnt = 0;
            int GuarantorCnt = 0;
            ViewBag.ShowTemps = true;

            ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(model.LoanId), "1", model.SelectedDeptText, out TemplateCnt, out GuarantorCnt);

            if (TemplateCnt > 0)
                model.Templates = tg.Templates;
            else
                model.ErrorT = "Templates not available for this LoanType.";

            if (GuarantorCnt > 0)
                model.Guarantors = tg.Guarantors;
            else
                model.ErrorG = "Guarantors not available for this LoanId.";

            return View("Index", model);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top