简体   繁体   English

无法呈现asp.net mvc部分视图

[英]cannot render asp.net mvc partial view

Using ajax I'm receiving on asp.net mvc controller action certain string. 使用ajax我在asp.net mvc控制器上收到某些字符串。 Based on that string value I want to render partial view. 基于该字符串值,我想呈现部分视图。

public ActionResult GetTabData(string activeTab)
        {
            string viewName = String.Empty;
            switch (activeTab)
            {
                case "all":
                    viewName = "_AllPartial";
                    break;
                case "one":
                    viewName = "_OnePartial";
                    break;
                case "two":
                    viewName = "_TwoPartial";
                default:
                    viewName = "_AllPartial";      
                    break;                    
            }               
            return PartialView("/Home/"+viewName);   
        }

All partial views are stored inside Views/Home directory but I'm constantly getting error that partial view cannot be found 所有局部视图都存储在Views / Home目录中,但是我不断收到错误消息,找不到局部视图

The partial view '/Home/_AllPartial' was not found or no view engine supports the searched locations. The following locations were searched:
/Home/_AllPartial

It's normal because the "Home" directory is not a location where your partial views should be stored. 这很正常,因为“主”目录不是应该存储部分视图的位置。

Partial views should be stored in your /Shared folder to make them work, however, If you want some organization in your project you can always write your own custom ViewEngine. 局部视图应存储在/ Shared文件夹中以使其起作用,但是,如果您希望项目中有某些组织,则始终可以编写自己的自定义ViewEngine。

Here's a sample: 这是一个示例:

public class ExtendedRazorViewEngine : RazorViewEngine
{
    #region Methods

    public void AddViewLocationFormat(string paths)
    {
        var existingPaths = new List<string>(ViewLocationFormats) {paths};

        ViewLocationFormats = existingPaths.ToArray();
    }

    public void AddPartialViewLocationFormat(string paths)
    {
        var existingPaths = new List<string>(PartialViewLocationFormats) {paths};

        PartialViewLocationFormats = existingPaths.ToArray();
    }

    #endregion
}

So, now in your Global.asax, you need to register this view engine. 因此,现在在您的Global.asax中,您需要注册此视图引擎。

 var engine = new ExtendedRazorViewEngine();
 engine.AddPartialViewLocationFormat("~/Views/Grids/{0}.cshtml");
 engine.AddPartialViewLocationFormat("~/Views/Modals/{0}.cshtml");
 ViewEngines.Engines.Add(engine);

In the example above, you see that I create a new engine, and that I specify 2 locations for my views. 在上面的示例中,您看到我创建了一个新引擎,并且为视图指定了2个位置。

This is working in my implementation so give it a try. 这在我的实现中起作用,因此请尝试一下。

Would this not work? 这行不通吗?

public ActionResult GetTabData(string activeTab)
    {
        string viewName = String.Empty;
        switch (activeTab)
        {
            case "all":
                viewName = "_AllPartial";
                break;
            case "one":
                viewName = "_OnePartial";
                break;
            case "two":
                viewName = "_TwoPartial";
            default:
                viewName = "_AllPartial";      
                break;                    
        }               
        return PartialView(string.concat("~/Views/Home/", viewName, ".cshtml");   
    }

We need to specify view file name extension(.cshtml/.aspx) also when you specifying directory. 指定目录时,我们还需要指定视图文件扩展名(.cshtml / .aspx)。

public ActionResult GetTabData(string activeTab)
    {
        string viewName = String.Empty;
        switch (activeTab)
        {
            case "all":
                viewName = "_AllPartial";
                break;
            case "one":
                viewName = "_OnePartial";
                break;
            case "two":
                viewName = "_TwoPartial";
            default:
                viewName = "_AllPartial";      
                break;                    
        }               
        return PartialView("~/Views/Home/"+viewName+".cshtml");   
    }

Place the view in the same view folder as the controller name, or the shared folder, and send the partial view, no "/Home" first. 将视图放置在与控制器名称相同的视图文件夹或共享文件夹中,然后发送局部视图,首先不发送“ / Home”。 It will automatically resolve the full path to the view. 它将自动解析视图的完整路径。

Also, partials are meant to be rendered inside a parent view. 同样,局部视图也应在父视图中呈现。 Why are you trying to return it on it's own? 您为什么要独自退货? Just use a standard view, and to get rid of the layout, just set a different layout depending on your needs: 只需使用标准视图,并摆脱布局,只需根据需要设置其他布局即可:

@{
    if (ViewBag.Modal != null && ViewBag.Modal) 
    { 
        Layout = "~/Views/Shared/_LayoutModal.cshtml";
    }
    else 
    { 
        Layout = "~/Views/Shared/_Layout.cshtml"; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM