C# – How to use Razor EditorFor Helper to create a TextBox with a value in from ViewBag

asp.net-mvcchtml-helperrazorviewbag

I simply want a text box editor field to appear with a value in it that has been passed with a ViewBag for example if ViewBag.Savings contained '6':

editor field

I tried something like:

@Html.EditorFor(model => model.Value, ViewBag.Savings)
@Html.ValidationMessageFor(model => model.Value)

I get the compiler error: 'Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'EditorFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.'

What's the correct syntax?

Best Answer

I think EditorFor needs a model ->

@model Foo.Models.Savings

then

@Html.EditorFor(model => model.Value)
@Html.ValidationMessageFor(model => model.Value)

For ViewBag you could do:

@Html.TextBox("Savings", (string)ViewBag.Savings)