【问题标题】:Radiobuttons instead of dropdownlist in mvc 3 application?单选按钮而不是 mvc 3 应用程序中的下拉列表?
【发布时间】:2011-07-23 02:05:23
【问题描述】:

我有一个视图,其中模型有一组项目。然后我有一个 EditorFor 模板,它负责为用户创建一个下拉列表,以便为集合中的每个项目选择有限数量的值之一:

@model Consultants.Models.ProgramSkill
<tr>
    <td>@Model.Program.Name
    </td>
        <td>@Model.Program.Category
    </td>
    <td>
        @Html.DropDownListFor( model => model.Level, new SelectList(new[] { 0, 1, 2, 3, 4, 5 }, Model.Level))
    </td>
</tr>

但我宁愿让单选按钮来做同样的事情,这在 MVC 3 中可能吗?如果有,怎么做?

【问题讨论】:

    标签: asp.net asp.net-mvc-3 drop-down-menu radio-button editorfor


    【解决方案1】:

    这将是自定义 html 助手的完美候选者:

    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    using System;
    
    public static class HtmlExtensions
    {
        public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, IEnumerable<SelectListItem> values)
        {
            string name = ExpressionHelper.GetExpressionText(ex);
            var sb = new StringBuilder();
            int counter = 1;
            foreach (var item in values)
            {
                sb.Append(htmlHelper.RadioButtonFor(ex, item.Value, new { id = name + counter.ToString()}));
                var label = new TagBuilder("label");
                label.SetInnerText(item.Text);
                label.Attributes.Add("for", name + counter.ToString());
                sb.Append(label.ToString());
                counter++;
            }
            return MvcHtmlString.Create(sb.ToString());
        }
    }
    

    型号:

    public class MyViewModel
    {
        public IEnumerable<SelectListItem> Items { get; set; }
        public string Level { get; set; }
    }
    

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Level = "2",
                Items = Enumerable.Range(1, 5).Select(x => new SelectListItem
                {
                    Value = x.ToString(), 
                    Text = "item " + x
                })
            };
            return View(model);
        }
    }
    

    还有一个观点:

    @model AppName.Models.MyViewModel
    
    @using (Html.BeginForm())
    {
        @Html.RadioButtonListFor(x => x.Level, Model.Items)
        <input type="submit" value="OK" />
    }
    

    【讨论】:

    • 我修改了上面的 RadioButtonFor() 方法以允许预先选择一个单选按钮项目(这只是设置选中属性的问题): sb.Append(htmlHelper.RadioButtonFor(ex, item.值, (item.Selected ? new { Checked = "" } : null)));
    【解决方案2】:
    @Html.RadioButtonFor(m => m.Level, 0)
    @Html.RadioButtonFor(m => m.Level, 1)
    @Html.RadioButtonFor(m => m.Level, 2)
    @Html.RadioButtonFor(m => m.Level, 3)
    @Html.RadioButtonFor(m => m.Level, 4)
    @Html.RadioButtonFor(m => m.Level, 5)
    

    或者用一个简单的循环:

    @for(int level = 0; level <= 5; level++)
    @Html.RadioButtonFor(m => m.Level, level)
    }
    

    【讨论】:

    • 但是有了这个,如何设置当前选中的Level呢? (就像我对下拉列表所做的那样)
    • 如果 Level 是 int ,它将使用 RadioButtonFor 自动选择
    猜你喜欢
    • 2018-03-25
    • 2015-07-30
    • 2015-06-30
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多