Asp.net mvc 3 MVC3-来自数据库的Radio按钮分组和绑定

Asp.net mvc 3 MVC3-来自数据库的Radio按钮分组和绑定,asp.net-mvc-3,Asp.net Mvc 3,如何使用MVC3 Razor从数据库中获取单选按钮选项。每个问题我有4个选项,这些选项应该从数据库中填充,并且应该分组 @Html.RadioButtonFor(model => Model.AAAa, 'Y', new { title = "Please check this if AAA has been updated" }) Yes 这给了我硬编码的值为Yes,但文本需要填充DB Table 如何将其绑定回选定的值并将其绑定回数据库?。举个例子会

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

 @Html.RadioButtonFor(model => Model.AAAa, 'Y', new { title = "Please check this if AAA has been updated" })
                Yes
这给了我硬编码的值为Yes,但文本需要填充DB Table

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


感谢您

一如既往,首先定义一个视图模型,该模型将表示您将在视图中处理的信息:

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>
}
@model MyViewModel
@使用(Html.BeginForm())
{
对于(int i=0;ix.SelectedValue,Model.Items[i].Value,新的{id=“item\+i})
@Html.Label(“item_”+i,Model.Items[i].Text)

} 好啊 }
或者,为了减少视图的混乱,您可以编写一个自定义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());
    }
}
public静态类
{
公共静态IHtmlString RadioButtonListFor(
这个HtmlHelper HtmlHelper,
表情表情,
数不清的项目
)
{
var sb=新的StringBuilder();
var i=0;
foreach(项目中的var项目)
{
var id=string.Format(“项{0}”,i++);
var radio=htmlhelp.RadioButtonFor(表达式,item.Value,新的{id=id});
var label=htmlHelper.label(id,item.Text);
sb.AppendFormat(“{0}{1}
”,收音机,标签); } 返回新的HtmlString(sb.ToString()); } }
现在你的观点将变成:

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.RadioButtonListFor(x => x.SelectedValue, Model.Items)
    <button type="submit">OK</button>
}    
@model MyViewModel
@使用(Html.BeginForm())
{
@RadioButtonListFor(x=>x.SelectedValue,Model.Items)
好啊
}    
很明显,它看起来更漂亮