I have this action method in C#:
public ActionResult Index() {
ViewBag.Message = \"Hello\";
return View();
}
And this
Instead of
x?ViewBag <- "Hello"
Try:
x.ViewBag?Message <- "Hello"
Just to provide a similar example, the MvcMovie example in the ASP.NET Core docs uses the following controller method:
public IActionResult Welcome(string name, int numTimes = 1)
{
ViewData["Message"] = "Hello " + name;
ViewData["NumTimes"] = numTimes;
return View();
}
converted to F#:
member this.Welcome (name : string, num_times : int) =
this.ViewData.Add("Message", box ("Hello " + name))
this.ViewData.Add("NumTimes", box num_times)
this.View()
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"