如何在 ASP.NET MVC 中创建几个单选框?
是的:(简单的问题*脸红*
让我们想象一下我有以下枚举:-
string[] languages = new string[]
{
"en (uk)", // English (colour)
"en (us)", // English (color)
"fr", // French
"es" // Spanish
};
ViewData["Languages"] = languages;
ViewData["UserLanguage"] = "en (uk)";
有了这个,我将如何在视图中显示单选按钮?这是枚举所有内容的唯一方法语言值并渲染RadioBox?
例如
<% foreach(string language in ViewData["Languages"] as string[])
{
response.write Html.RadioBox(... not sure what to set in here ...)
}%>
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速而肮脏:
问题是您在该视图数据中分配了魔术字符串。 我要做的是这样的:
创建一个类:UserForm
视图应该是强类型的并且类型为:UserForm
然后你可以像这样渲染它:
从控制器你会:
HTH。
编辑:
好的,找到了 -
RadioButtonList
是Microsoft.Web.Mvc
命名空间(MVCFutures 项目)中的一个扩展方法 - 您可以从这里获取它:MVCFuturesQuick and dirty:
The problem is that you have allot of magic strings in that viewdata. What I would do is something like this:
Create a class: UserForm
the view should be strongly typed and be of type : UserForm
then you could render it like :
From the controller you would :
HTH.
EDIT:
OK, found it - the
RadioButtonList
is an extension method in theMicrosoft.Web.Mvc
namespace (the MVCFutures project) - you can get it from here : MVCFutures