asp.net-mvc MVC Razor HTML Helper 语法:Html.Hidden 对象参数中的 Viewbag

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10179015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:52:12  来源:igfitidea点击:

MVC Razor HTML Helper syntax: Viewbag in Html.Hidden object parameter

asp.net-mvcrazor

提问by Kman

I am trying to add a object to my Html.Hidden HTML helper, but I can't get the syntax quite right.

我正在尝试将一个对象添加到我的 Html.Hidden HTML 助手中,但我无法完全正确地使用语法。

Syntax 1:

语法 1:

@Html.Hidden("hiddenDate", ViewBag.myDate.ToString("dd.MM.yyyy"))

Results in runtime error and it can't resolve the @Html.Hidden in view.

导致运行时错误,并且无法解析视图中的 @Html.Hidden。

Syntax 2:

语法 2:

@Html.Hidden("hiddenDate", new { String = ViewBag.myDate.ToString("dd.MM.yyyy")})

Sets the value="{ String = 16.04.2012 }"

设置值="{ String = 16.04.2012 }"

I would like to get the value to only "16.04.2012", but no success after several syntax tweaks

我想将值设置为“16.04.2012”,但经过多次语法调整后没有成功

回答by Ropstah

Try casting the return value to object:

尝试将返回值转换为object

@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy"))