asp.net-mvc 在 Razor 视图引擎和 ASP.NET MVC 3 中使用动态模型渲染局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4047543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:32:54  来源:igfitidea点击:

Render partial view with dynamic model in Razor view engine and ASP.NET MVC 3

asp.net-mvcrazorasp.net-mvc-3

提问by Diego

When I try to render a partial view whose model type is specified as:

当我尝试渲染模型类型指定为的局部视图时:

@model dynamic

by using the following code:

通过使用以下代码:

@{Html.RenderPartial("PartialView", Model.UserProfile);}

I get the following exception:

我收到以下异常:

'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'RenderPartial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

However, the same code in a .aspx file works flawlessly. Any thoughts?

但是,.aspx 文件中的相同代码可以完美运行。有什么想法吗?

采纳答案by Diego

Just found the answer, it appears that the view where I was placing the RenderPartial code had a dynamic model, and thus, MVC couldn't choose the correct method to use. Casting the model in the RenderPartial call to the correct type fixed the issue.

刚刚找到答案,看来我放置RenderPartial代码的视图有一个动态模型,因此MVC无法选择正确的方法来使用。将 RenderPartial 调用中的模型转换为正确的类型修复了该问题。

source: Using Html.RenderPartial() in ascx files

来源:在 ascx 文件中使用 Html.RenderPartial()

回答by juan

Instead of casting the model in the RenderPartial call, and since you're using razor, you can modify the first line in your view from

而不是在 RenderPartial 调用中投射模型,并且由于您使用的是剃刀,您可以修改视图中的第一行

@model dynamic

to

@model YourNamespace.YourModelType

This has the advantage of working on every @Html.Partialcall you have in the view, and also gives you intellisense for the properties.

这具有处理@Html.Partial视图中的每个调用的优点,并且还为您提供属性的智能感知。

回答by Tom

Can also be called as

也可以称为

@Html.Partial("_PartialView", (ModelClass)View.Data)

回答by J Wynia

There's another reason that this can be thrown, even if you're not using dynamic/ExpandoObject. If you are doing a loop, like this:

即使您没有使用动态/ExpandoObject,也有另一个原因可以抛出这个。如果你在做一个循环,像这样:

@foreach (var folder in ViewBag.RootFolder.ChildFolders.ToList())
{
    @Html.Partial("ContentFolderTreeViewItems", folder)
}

In that case, the "var" instead of the type declaration will throw the same error, despite the fact that RootFolder is of type "Folder. By changing the var to the actual type, the problem goes away.

在这种情况下,“var”而不是类型声明会抛出相同的错误,尽管 RootFolder 的类型是“Folder”。通过将 var 更改为实际类型,问题就会消失。

@foreach (ContentFolder folder in ViewBag.RootFolder.ChildFolders.ToList())
{
    @Html.Partial("ContentFolderTreeViewItems", folder)
}

回答by Segev -CJ- Shmueli

Here's a way to pass a dynamic object to a view (or partial view)

这是将动态对象传递给视图(或部分视图)的方法

Add the following class anywhere in your solution (use System namespace, so its ready to use without having to add any references) -

在解决方案中的任何位置添加以下类(使用 System 命名空间,因此无需添加任何引用即可使用)-

    namespace System
    {
        public static class ExpandoHelper
        {
            public static ExpandoObject ToExpando(this object anonymousObject)
            {
                IDictionary<string, object> anonymousDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(anonymousObject);
                IDictionary<string, object> expando = new ExpandoObject();
                foreach (var item in anonymousDictionary)
                    expando.Add(item);
                return (ExpandoObject)expando;
            }

        }
    }

When you send the model to the view, convert it to Expando :

当您将模型发送到视图时,将其转换为 Expando :

    return View(new {x=4, y=6}.ToExpando());

Cheers

干杯

回答by RousseauAlexandre

I had the same problem & in my case this is what I did

我遇到了同样的问题,就我而言,这就是我所做的

@Html.Partial("~/Views/Cabinets/_List.cshtml", (List<Shop>)ViewBag.cabinets)

and in Partial view

并在部分视图中

@foreach (Shop cabinet in Model)
{
    //...
}

回答by Alan

I was playing around with C# code an I accidentally found the solution to your problem haha

我在玩 C# 代码,无意中找到了解决您问题的方法哈哈

This is the code for the Principal view:

这是主视图的代码:

`@model dynamic 
 @Html.Partial("_Partial", Model as IDictionary<string, object>)`

Then in the Partial view:

然后在局部视图中:

`@model dynamic 
 @if (Model != null) { 
   foreach (var item in Model) 
   { 
    <div>@item.text</div> 
   } 
  }`

It worked for me, I hope this will help you too!!

它对我有用,我希望这也能帮助你!!