2

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
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
sagesky36
  • 4,542
  • 19
  • 82
  • 130
  • Can you include the controller code where you call `return View("Home", model);`? – Gromer Oct 04 '12 at 22:05
  • My code is included at the bottom part of my edit.. If there is another place I am supposed to put the addition of edits, pelase let me know. Let me know if you don't see the edits (at the bottom of my post) asap. Because I just posted it... Thanks so much for taking a look at it... – sagesky36 Oct 04 '12 at 22:24
  • There must be a way to fix this somehow within the project and inform it what the default View is. – sagesky36 Oct 04 '12 at 23:03
  • You can cleary see why it's causing the error, but I don't know why... – sagesky36 Oct 04 '12 at 23:05
  • The msg is looking for "Home" as the action method while inside the Global.asax file it clearly specifies the following: How can I correct this: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); – sagesky36 Oct 04 '12 at 23:06
  • I may not be understanding your question...It is looking in ~/Views/Home/Home.aspx because you have `return View("Home", model);`. The logic for this is simple - it searches for a file named "home" in a folder named ~/Views/{controller name}/{view name}, if that can't be found it looks for ~/Views/Shared/{view name}. If you want to return the "Index" view from your home controller use `return View("Index", model);`. You can also specify the location `return View("~/Views/Home/Index.cshtml", model);`. Maybe this helps? – christophersw Oct 05 '12 at 15:04

1 Answers1

7

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;
    }
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480