简体   繁体   English

如何使用Razor EditorFor Helper创建带有ViewBag中的值的文本框

[英]How to use Razor EditorFor Helper to create a TextBox with a value in from ViewBag

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': 我只希望文本框编辑器字段中显示一个值,该值已随ViewBag传递,例如,如果ViewBag.Savings包含'6':

编辑栏

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. 我收到编译器错误:' 编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper'没有名为'EditorFor'的适用方法,但似乎具有该名称的扩展方法。 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? 正确的语法是什么?

I think EditorFor needs a model -> 我认为EditorFor需要模型->

@model Foo.Models.Savings

then 然后

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

For ViewBag you could do: 对于ViewBag,您可以执行以下操作:

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

The EditorFor Html Helper wants a strongly typed object which a ViewBag is not. Html Helper EditorFor器需要一个ViewBag不需要的强类型对象。 If you really want to use a ViewBag you would do something like this: 如果您真的想使用ViewBag,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM