如何在MVC剃刀中创建动态单选按钮 [英] how to create dynamic radio buttons in mvc razor

查看:43
本文介绍了如何在MVC剃刀中创建动态单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常需要创建动态单选按钮.

I have a complex need to create dynamic radio buttons.

理论上,您有类别,而这些类别中有项目. 类别是动态的,项目也是动态的.

The theory is you have categories and these categories have items. The categories are dynamic and so are their items.

在我的模特中,我有...

In my model i have...

public IList> ItemCategories {get;放; }

public IList> ItemCategories { get; set; }

但是我不确定这是否是创建radioFor按钮的正确方法吗?

but I'm not sure if this is the correct way how to create the radioFor button?

帮助?

我最初的想法是...

My initial Idea was...

//型号

public IList<Category> DynamicCategories { get; set; }

public IList<long> DynamicCategoryItems { get; set; }

//HTML

@for (int i = 0; i < Model.DynamicCategories.Count; i++)
{
      @Html.EditorFor(model => model.DynamicCategories[i], "DynamicCategories", new { Index = i, IsHidden = false })
}

//编辑器

@model Category
@{
    Entities.Category rowModel = new Entities.Category();
    int count = ViewBag.Index == null ? 0 : (int)ViewBag.Index;
}

<h3>@Model.Name</h3>
<div class="options">
    @foreach (CategoryItem item in Model.CategoryItems.Where(x => x.Enabled))
    {
        <div class="option" data-search-text="@item.Name">
            @item.Name 
            <input type="radio" name="DynamicCategoryItems[@count]" value="@item.Id" @(item.Selected ? "checked" : "")/>
        </div>            
    }
    <div class="clear"></div>
</div>              

推荐答案

尝试一下

for (int i = 0; i < Model.DynamicCategories.Count; i++)
{
    @Html.RadioButtonFor(model => model.DynamicCategories[i],model => model.DynamicCategoryItems[i]) @:Text

}

这里的文本是单选按钮的文本.

here Text is text for radiobutton.

这篇关于如何在MVC剃刀中创建动态单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆