假设我有以下类:
class FooViewModel
{
// ...Other scalar properties
public IEnumerable<BarViewModel> Bars { get; set; }
}
class BarViewModel
{
// ...Other scalar properties
public IEnumerable<BazViewModel> Bazs { get; set; }
}
class BazViewModel
{
// ...Other scalar properties
}
我有一个FooViewModel
实例作为模型的视图。我使用@Html.NameFor(m => m.Bars)
从我的视图中获取值Bars
。现在我尝试从我的视图中获取值Bazs
,并尝试了以下内容:
<script>
// bazs returns empty string
var bazs = '@Html.NameFor(m => m.Bars.Select(bar => bar.Bazs))';
// bazs returns empty string as well
var bazs = '@Html.NameFor(m => m.Bars.SelectMany(bar => bar.Bazs))';
</script>
如何获取字符串值Bazs
(来自FooViewModel
)并将其呈现在javascript变量中?