mvc 3 应用程序中使用单选按钮而不是下拉列表?

发布于 2024-10-26 13:33:25 字数 469 浏览 7 评论 0原文

我有一个视图,其中模型具有项目集合。然后我有一个 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 中可能吗?如果是这样,怎么办?

I have a View where the model has a collection of items. Then I have an EditorFor template that takes care of creating a dropdownlist for the user to select one of a limited number of values for each item in the collection:

@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>

But I would rather have radiobuttons to do the same thing, is that possible in MVC 3? If so, how?

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

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

发布评论

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

评论(2

沫雨熙 2024-11-02 13:33:25

这将是自定义 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" />
}

That would be a perfect candidate for a custom html helper:

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());
    }
}

Model:

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

Controller:

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);
    }
}

and a view:

@model AppName.Models.MyViewModel

@using (Html.BeginForm())
{
    @Html.RadioButtonListFor(x => x.Level, Model.Items)
    <input type="submit" value="OK" />
}
木有鱼丸 2024-11-02 13:33:25
@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)
}
@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)

or with a simple loop:

@for(int level = 0; level <= 5; level++)
@Html.RadioButtonFor(m => m.Level, level)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文