如何在 ASP.NET MVC 中创建几个单选框?

发布于 2024-07-15 17:39:05 字数 677 浏览 3 评论 0 原文

是的:(简单的问题*脸红*

让我们想象一下我有以下枚举:-

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 ...)
}%>

yeah :( simple question * blush *

Lets imagine i have the following enumeration:-

string[] languages = new string[]
                     {
                         "en (uk)", // English (colour)
                         "en (us)", // English (color)
                         "fr", // French
                         "es" // Spanish
                     };

ViewData["Languages"] = languages;
ViewData["UserLanguage"] = "en (uk)";

Armed with this, how would i display a radio button in the View? Is the only way to do this to enumerate through all the language values and render a RadioBox?

eg. pseduo-code...

<% foreach(string language in ViewData["Languages"] as string[])
{
  response.write Html.RadioBox(... not sure what to set in here ...)
}%>

cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-07-22 17:39:05

快速而肮脏:

<% foreach(string language in ViewData["Languages"] as string[]) { %>
  <%= Html.RadioBox(language,language,language == ViewData["UserLanguage"].ToString()) %>
<% } %>

问题是您在该视图数据中分配了魔术字符串。 我要做的是这样的:

  1. 创建一个类:UserForm

    类用户表单{ 
    
           IList<字符串>   _语言; 
           字符串_selectedL; 
    
           public UserForm(IList languages, string selectedLanguage) 
           { 
             _languages = 语言; 
             _selectedL = 选定语言; 
           } 
    
           IEnumerable;   用户语言{ 
               得到 { 
                    从 _languages 中的 l 返回 
                            选择新的SelectedListItem { 
                                文本 = l, 
                                值=l, 
                                选定 = (l == _selectedL) 
                            }; 
               } 
           } 
      } 
      
  2. 视图应该是强类型的并且类型为:UserForm
    然后你可以像这样渲染它:

    <%= Html.RadioButtonList("userLanguages",ViewData.Model.UserLanguages) %> 
      
  3. 从控制器你会:

    return View(new UserForm(listOfLanguages, selectedLanguage)); 
      

HTH。

编辑:
好的,找到了 - RadioButtonListMicrosoft.Web.Mvc 命名空间(MVCFutures 项目)中的一个扩展方法 - 您可以从这里获取它:MVCFutures

Quick and dirty:

<% foreach(string language in ViewData["Languages"] as string[]) { %>
  <%= Html.RadioBox(language,language,language == ViewData["UserLanguage"].ToString()) %>
<% } %>

The problem is that you have allot of magic strings in that viewdata. What I would do is something like this:

  1. Create a class: UserForm

    class UserForm {
    
         IList<string> _languages;
         string _selectedL;
    
         public UserForm (IList<string> languages, string selectedLanguage)
         {
           _languages = languages;
           _selectedL = selectedLanguage;
         }
    
         IEnumerable<SelectedListItem> UserLanguages {
             get {
                  return from l in _languages
                          select new SelectedListItem {
                              Text = l,
                              Value = l,
                              Selected = (l == _selectedL)
                          };
             }
         }
    }
    
  2. the view should be strongly typed and be of type : UserForm
    then you could render it like :

    <%= Html.RadioButtonList("userLanguages",ViewData.Model.UserLanguages) %>
    
  3. From the controller you would :

    return View(new UserForm(listOfLanguages, selectedLanguage));
    

HTH.

EDIT:
OK, found it - the RadioButtonList is an extension method in the Microsoft.Web.Mvc namespace (the MVCFutures project) - you can get it from here : MVCFutures

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文