Set a property on ViewBag dynamic object in F#

眉间皱痕 提交于 2019-12-21 02:28:16

问题


I have this action method in C#:

  public ActionResult Index() {
      ViewBag.Message = "Hello";
      return View();
  }

And this view (Index.cshtml):

  <h2>@ViewBag.Message</h2>

And this produces the expected "Hello" on the page.

I want to do the controller in F#. I've tried

type MainController() =
  inherit Controller()
  member x.Index() =
    x.ViewBag?Message <- "Hello"
    x.View()

And this produces the error message "Method or object constructor 'op_DynamicAssignment' not found".

I've looked at some of the F# code samples for the dynamic operator, and I can't see anything that's shorter than several pages of description and many lines of code. They seem to be too general for just this property "setter".


回答1:


The ViewBag property is just a wrapper that exposes the ViewData collection as a property of type dynamic, so that it can be accessed dynamically from C# (using property set syntax). You could use implementation of ? based on DLR to do that (see this discussion at SO), but it is easier to define ? operator that adds data directly to ViewDataDictionary (which is exposed by the ViewData property):

let (?<-) (viewData:ViewDataDictionary) (name:string) (value:'T) =
  viewData.Add(name, box value)

Then you should be able to write

x.ViewData?Message <- "Hello"



回答2:


Instead of

x?ViewBag <- "Hello"

Try:

x.ViewBag?Message  <- "Hello"


来源:https://stackoverflow.com/questions/8149127/set-a-property-on-viewbag-dynamic-object-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!