【问题标题】:is there a way in asp.net mvc 4 to create an Index Action inside Base Controller and Share the view of this Action to multiple Controllers?asp.net mvc 4 中有没有办法在基本控制器中创建索引操作并将此操作的视图共享给多个控制器?
【发布时间】:2014-09-15 18:51:14
【问题描述】:

我有这个控制器:

public class BaseController : Controller
    {    
        public ActionResult Index(string controller)
        {
            TemplateMasterModel model = new TemplateMasterModel()
            return View(model);
        }
    }

我为 baseController 索引操作创建了一个视图

它位于 Views/Base/Index.cshtlm

代码:

@model Reports.Models.TemplateMasterModel

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

@Html.Partial("TemplateRptMaster",Model)

@section scripts{
    @Scripts.Render("~/bundles/rptFormSubmit")
}

我想知道是否有办法将此索引视图用于继承 BaseController 的其他控制器,它返回以下错误:

未找到视图“索引”或其主视图或没有视图引擎 支持搜索的位置。以下位置是 搜索:

【问题讨论】:

    标签: c# asp.net-mvc-4


    【解决方案1】:

    您也可以将 index.cshtml 视图放入 Shared 文件夹中。如果您不想弄乱您的共享文件夹,并且想要其他方案,您将不得不编写自己的视图引擎,这听起来比它更复杂......这是一个链接:http://www.dotnet-tricks.com/Tutorial/mvc/NcJP200113-Custom-Razor-View-Engine-for-C

    【讨论】:

    • 谢谢伙计,太棒了,我所做的只是为我的特定 MVC 区域编写自己的视图引擎,它没有任何问题。 我的代码: ` public class MyViewEngine : RazorViewEngine { public MyViewEngine() { AreaViewLocationFormats = new[] { "~/Areas/Rpts/Views/Base/Index.cshtml" }; } } ` 引擎必须在 Global.asax 文件中注册。 这边: ViewEngines.Engines.Add(new MyViewEngine());
    猜你喜欢
    • 2013-02-04
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    相关资源
    最近更新 更多