【问题标题】:MVC3- Radion Button Grouping and Binding from databaseMVC3- 来自数据库的单选按钮分组和绑定
【发布时间】:2012-01-24 14:33:22
【问题描述】:

如何使用 MVC3 Razor 从数据库中获取单选按钮选项。每个问题我有 4 个选项,这些选项应该从数据库中填充并且应该分组。

 @Html.RadioButtonFor(model => Model.AAAa, 'Y', new { title = "Please check this if AAA has been updated" })
                Yes

这给了我硬编码值为 Yes,但文本需要用 DB Table 填充。

我如何将它绑定回选定的值回数据库?举个例子会更有帮助。

谢谢

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    与往常一样,您首先定义一个视图模型,该模型将代表您将在视图中处理的信息:

    public class MyViewModel
    {
        public string SelectedValue { get; set; }
    
        // In your question you seem to be dealing with a title attribute as well
        // If this is the case you could define a custom view model ItemViewModel
        // which will contain the Value, the Text and the Title properties for
        // each radio button because the built-in SelectListItem class that I am using here
        // has only Value and Text properties
        public SelectListItem[] Items { get; set; }
    }
    

    然后你写一个控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                // Obviously those will come from your database or something
                Items = new[]
                {
                    new SelectListItem { Value = "Y", Text = "Yes" },
                    new SelectListItem { Value = "N", Text = "No" },
                    new SelectListItem { Value = "D", Text = "Dunno" }
                }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    然后是对应的视图:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        for (int i = 0; i < Model.Items.Count; i++)
        {
            @Html.RadioButtonFor(x => x.SelectedValue, Model.Items[i].Value, new { id = "item_" + i })
            @Html.Label("item_" + i, Model.Items[i].Text)
            <br/>
        }
        <button type="submit">OK</button>
    }
    

    或者为了让视图不那么凌乱,你可以编写一个自定义的 HTML 帮助器来为你呈现这些单选按钮:

    public static class HtmlExtensions
    {
        public static IHtmlString RadioButtonListFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper, 
            Expression<Func<TModel, TProperty>> expression, 
            IEnumerable<SelectListItem> items
        )
        {
            var sb = new StringBuilder();
            var i = 0;
            foreach (var item in items)
            {
                var id = string.Format("item{0}", i++);
                var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id });
                var label = htmlHelper.Label(id, item.Text);
                sb.AppendFormat("{0}{1}<br/>", radio, label);
            }
    
            return new HtmlString(sb.ToString());
        }
    }
    

    现在您的视图将变成:

    @model MyViewModel
    
    @using (Html.BeginForm())
    {
        @Html.RadioButtonListFor(x => x.SelectedValue, Model.Items)
        <button type="submit">OK</button>
    }    
    

    这显然更好看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多