Dynamic Anonymous type in Razor causes RuntimeBinderException

后端 未结 12 939
不思量自难忘°
不思量自难忘° 2020-11-22 07:45

I\'m getting the following error:

\'object\' does not contain a definition for \'RatingName\'

When you look at the anonymous dyn

12条回答
  •  萌比男神i
    2020-11-22 08:07

    I tried the ExpandoObject but it didn't work with a nested anonymous complex type like this:

    var model = new { value = 1, child = new { value = 2 } };
    

    So my solution was to return a JObject to View model:

    return View(JObject.FromObject(model));
    

    and convert to dynamic in .cshtml:

    @using Newtonsoft.Json.Linq;
    @model JObject
    
    @{
        dynamic model = (dynamic)Model;
    }
    Value of child is: @model.child.value
    

提交回复
热议问题