gpt4 book ai didi

asp.net-mvc - 通过 F# 代码遍历插入到 ASP.NET MVC4 页面的 ViewData 中的字典? ( Razor )

转载 作者:行者123 更新时间:2023-12-04 19:06:50 24 4
gpt4 key购买 nike

说我疯了,但我正在用 F# 开发一个 ASP.NET MVC4 站点。这是我的第一个障碍;在 Controller 中,我提交了一个字典,这样:

[<HandleError>]
type FooController() =
inherit Controller()
member this.Index () =
this.ViewData.Add("Foo", "Bar")
this.ViewData.Add("Baz", dict [ (0, 0); (1, 3); (2, 2); ] )
this.View() :> ActionResult

现在我想遍历 Razor 模板中的值,该怎么做?我试过这个:
@foreach (var time in @ViewBag.Baz.Keys)
{
<hr /><p>Time is @time, and value is @ViewBag.Baz[time]</p>
}

但它抛出这个错误:
Line 119:                    Total: <span class="highlight">6449</span> kW/h.
Line 120: </p>
Line 121: @foreach (var time in (@ViewBag.Baz).Keys)
Line 122: {
Line 123: <hr /><p>Time is @time, and value is @ViewBag.Baz[time]</p>

[RuntimeBinderException: 'object' does not contain a definition for 'Keys']
CallSite.Target(Closure , CallSite , Object ) +280
System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +924
ASP._Page_Views_lobby_Index_cshtml.Execute() in c:\Users\knocte\documents\visual studio 2012\projects\fsolar\conclavesolarweb\conclavesolarwebwebapi\Views\Lobby\Index.cshtml:121
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +126
System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +181
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +853420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +837892
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

最佳答案

因此,您遇到的问题是动态关键字和 explicit interface implementations 的已知限制。 .不幸的是Map也会有这个问题。如果你真的想使用这样的 ViewBag,你可以使用一个实际的 Dictionary<int,int> .

但最终,您应该发挥 F# 的优势,即静态类型和低仪式类型创建。在 F# 代码中创建轻量类型并使用 @model keyword在你看来。

关于asp.net-mvc - 通过 F# 代码遍历插入到 ASP.NET MVC4 页面的 ViewData 中的字典? ( Razor ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com