2

尝试返回视图时,我在使用 MVC 4 应用程序时遇到零星错误。

在这种特殊情况下,我将返回一个视图return View("Home", model);,这就是我得到消息的地方。当您不断测试和调试时,它似乎也偶尔发生,我认为 View Engine 变得疯狂。比如在这个之前,我在执行一个简单的视图时说它一直在那里找不到它。在清除缓存、重新启动等并执行相同的确切逻辑之后,它起作用了。

所以,我不知道如何用视图引擎解决这个问题。在我回到视图之前,我可以向你保证,我的模型中有记录。我无法通过我的电脑在此表单上附加屏幕打印 --- 没有选择。

所以,可怕的问题是:我如何解决这个问题,它不会像这样偶尔发生???这是一个严重的问题,希望能解决它....

我查看了 Scott Hanselmans 用于预编译视图等的 nuget 包,但似乎过于复杂和额外的工作。我想知道是否还有其他我可以做的事情。

任何帮助将非常感激....

这是我的 Globla.asax 文件:

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 }
    );
}

如果有人可以回答,我将不胜感激,因为这将使我们正在开发的 MVC 应用程序停止运行!

我尝试.DataTokens.Add("area", "YOURAREANAME");在 的末尾添加MapRoute,但我不知道用什么来代替字符串。

另外,我不知道为什么需要这样做(如果它会解决它)并且需要某人的解释......

为想要检查控制器代码的另一个人添加了代码。

[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;
            }
        }

当我的目录结构清晰时,我不明白为什么路由引擎会尝试转到以下内容:Views/Home/Index.cshtml

给出的错误如下,甚至没有寻找“索引”页面。

~/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
4

1 回答 1

7

替换这个:

return View("Home", model);

有了这个:

return View("Index", model);

完整代码:

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;
    }
}
于 2012-10-05T16:44:03.043 回答