kno*_*cte 4 asp.net-mvc f# razor asp.net-mvc-4
叫我疯了,但我正在用F#开发一个ASP.NET MVC4站点.这是我的第一个障碍; 在控制器中我提交字典,这样:
[<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
Run Code Online (Sandbox Code Playgroud)
现在我想迭代Razor模板中的值,该怎么做?我试过这个:
@foreach (var time in @ViewBag.Baz.Keys)
{
<hr /><p>Time is @time, and value is @ViewBag.Baz[time]</p>
}
Run Code Online (Sandbox Code Playgroud)
但它抛出了这个错误:
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
Run Code Online (Sandbox Code Playgroud)
因此,您遇到的问题是动态关键字和显式接口实现的已知限制.不幸的是Map也会有这个问题.如果你真的想使用像这样的ViewBag,你可以使用实际的Dictionary<int,int>.
最终,你应该发挥F#的优势,即静态打字和低礼式类型创作.在F#代码中创建轻量级类型,并在视图中使用@model关键字.