Html.Editor 不渲染值
我在使 Html.Editor 渲染所需的 HTML 时遇到问题。
以下是场景:
// assign the value
ViewBag.BeginDate = seaBeginEnd.beginDate;
//View
@Html.Editor("Begin", ViewBag.BeginDate as DateTime?)
//HTML Source
<div class="editor-field">
<input class="text-box single-line" id="Begin" name="Begin" type="text" value="" />
</div>
我想看到 1/19/2011 12:00:00 AM 的值,这是 ViewBag.BeginDate 的值,任何见解。
感谢您的帮助!
I'm having problems making the Html.Editor rendering the desire HTML.
Here is the scenario:
// assign the value
ViewBag.BeginDate = seaBeginEnd.beginDate;
//View
@Html.Editor("Begin", ViewBag.BeginDate as DateTime?)
//HTML Source
<div class="editor-field">
<input class="text-box single-line" id="Begin" name="Begin" type="text" value="" />
</div>
I was specking to see a value of 1/19/2011 12:00:00 AM which is the value of ViewBag.BeginDate, any insights.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能通过将
Begin
作为第一个参数传递给来期望这样的事情Html.Editor 帮助器。第二个参数并没有像你想象的那样做。它只是将一些额外的视图数据发送到编辑器模板,但您绑定到的原始值称为
Begin
,因此您应该为其分配值。像这样:然后:
显然,每次我看到有人使用 ViewBag/ViewData 而不是强类型助手时,我都觉得有义务推荐视图模型和强类型助手。示例:
模型:
控制器:
以及相应的强类型视图:
You cannot expect such thing by passing
Begin
as first parameter to theHtml.Editor
helper. The second parameter doesn't do what you think it does. It simply sends some additional view data to the editor template but the original value you are binding to is calledBegin
so that's what you should assign a value to. Like this:and then:
Obviously every time I see someone using ViewBag/ViewData and not strongly typed helpers I feel in the obligation to recommend view models and strongly typed helpers. Example:
Model:
Controller:
and the corresponding strongly typed view: