簡體   English   中英

嘗試訪問Controller Function時獲取InvalidOperationException以訪問View

[英]Getting an InvalidOperationException for accessing View when trying to access Controller Function

支持代碼對MVC來說還很陌生,通常可以使用,但是會產生過多的服務器錯誤。 服務器錯誤采取以下形式:

Exception information: 
    Exception type: InvalidOperationException 
    Exception message: The view 'RenderProthyPDF' or its master was not     found or no view engine supports the searched locations. The following locations  were searched:
~/Views/Portal/RenderProthyPDF.aspx
~/Views/Portal/RenderProthyPDF.ascx
~/Views/Shared/RenderProthyPDF.aspx
~/Views/Shared/RenderProthyPDF.ascx
~/Views/Portal/RenderProthyPDF.cshtml
~/Views/Portal/RenderProthyPDF.vbhtml
~/Views/Shared/RenderProthyPDF.cshtml
~/Views/Shared/RenderProthyPDF.vbhtml
   at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at  System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext  controllerContext, ActionResult actionResult)

但是,“ RenderProthyPDF不是視圖,而是控制器內的功能:

cshtml中的示例調用代碼:

<a class="document-link" href="javascript:markLastVisitedCaseDocument('@currentDocument.WorkingDocumentURL'); renderProthyPDF('@currentDocument.DktYear', '@currentDocument.DktSequence', '@currentDocument.WorkingDocumentURL');">

<a class="tile" style="height: 130px;" href="javascript:renderProthyPDF('@d.DktYear', '@d.DktSequence', '@d.URL');">

引用的javascript:

    renderProthyPDF = function (rDktYear, rDktSequence, rURL) {
    jQuery.ajax({
        async: false,
        type: "POST",
        url: '@Url.Action("RenderProthyPDF", "Portal")',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ Year : rDktYear,
                           Sequence : rDktSequence,
                  prothyDocumentURL : rURL
        }),
        success: function (data) {
            alert(data);
        },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });

和C#代碼(在JPortal \\ Controllers \\ PortalController.cs中):

        public ActionResult RenderProthyPDF(string Year, string Sequence, string prothyDocumentURL, PortalCaseView viewmodel)
    {
        System.Web.HttpContext.Current.Session.Add("renderingPDF", "true");
        System.Web.HttpContext.Current.Session.Add("renderingURL", prothyDocumentURL);

        return View();
    }

我應該怎么做才能防止這些錯誤的產生? 怎么了?

編輯:我指定目錄我有:

引用的超鏈接在JPortal \\ Views \\ Shared_.cshtml的部分視圖中

主視圖(JavaScript代碼)位於:JPortal \\ Views \\ Portal \\ Case.cshtml

ActionResult函數位於JPortal \\ Conrollers \\ PortalController.cs中

錯誤消息不夠清晰,因為它說它在搜索的以下路徑中找不到名為RenderProthyPDF.cshtml的視圖文件。

~/Views/Portal/*
~/Views/Shared/*

在控制器的Action方法RenderProthyPDF您說的是return View(); RenderProthyPDF 它將查找與無法找到的操作方法同名的視圖,從而導致錯誤。

如果沒有該視圖,則生成一個(或)如果使用其他名稱創建了該視圖,則將該名稱傳遞給View()方法,例如(因為View()方法的重載之一接受該視圖名稱作為string參數)

return View("MydifferentNamedRenderProthyPDF");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM