MVC Razor dynamic model, 'object' does not

2019-01-04 09:06发布

Using MVC 3 with Razor view engine. I have this View:

@model dynamic
@{
    var products = (List<ListItemBaseModel>)Model.Products;
    var threshold = (int)(Model.Threshold ?? 1);
    var id = Guid.NewGuid().ToString();
}

It is called from another view using this code:

@Html.Partial("PartialViewName", new { Products = Model, Threshold = 5 })

In both Views, when i debug them and watch Model, it seems to contain the correct object. When i execute the code i get an error on the "var products =" line saying:

'object' does not contain a definition for 'Products'

Can anyone explain to me why i get that error? Again, when i watch the Model object in debugging mode it looks all right (having 2 properties: Products and Threshold)

8条回答
再贱就再见
2楼-- · 2019-01-04 09:13

I worked around this issue by using a Dictionary.

 @Html.Partial("_Partial", new Dictionary<string, string> { { "Key1", "Val1" }, { "Key2", "Val2" }, { "Key3", "Val3" } });
查看更多
唯我独甜
3楼-- · 2019-01-04 09:14

Instead of using the dynamic Model type within the partial view.

You can call the anonymous object attributes using @ViewData.Eval("foo") instead of @Model.foo.

Then you can remove @Model dynamic from the view.

I came across this issue recently when passing some attributes between views for the Facebook Social Comments Integration. Example code:

Html.RenderPartial(@"Layouts/Partials/_Comments", new {currentUrl = Model.CurrentPage.GetAbsoluteUrl(), commentCount = 5 });

Then in my view I just had this div:

<div class="fb-comments" data-href="@ViewData.Eval("currentUrl")" data-numposts="@ViewData.Eval("commentCount")" data-width="100%"></div>
查看更多
The star\"
4楼-- · 2019-01-04 09:18

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

    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 :

    return View(new {x=4, y=6}.ToExpando());
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-04 09:19

To use dynamic type you need to reference Microsoft.CSharp assembly

查看更多
Emotional °昔
6楼-- · 2019-01-04 09:22

On .NET 4.0 Anonymous types can easily be converted to ExpandoObjects and thus all the problems are fixed with the overhead of the conversion itself. Check out here

查看更多
劫难
7楼-- · 2019-01-04 09:22

i am not sure that you are getting this error because you are not implementing the work-around. i got the same error in a partial view. the solution was just to clean the build and rebuild it. if the syntax is correct, the code should work, but the razor engine may not be updating the code changes properly.

查看更多
登录 后发表回答