Asp.net mvc 如何在mvc razor中创建动态单选按钮

Asp.net mvc 如何在mvc razor中创建动态单选按钮,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我需要创建动态单选按钮 理论是你有类别,这些类别有项目。 类别是动态的,它们的项目也是动态的 在我的模型中我有 公共IList>ItemCategories{get;set;} 但我不确定这是否是创建radioFor按钮的正确方法 帮忙 我最初的想法是 //模型 public IList<Category> DynamicCategories { get; set; } public IList<long> DynamicCategoryItems { get; set

我需要创建动态单选按钮

理论是你有类别,这些类别有项目。 类别是动态的,它们的项目也是动态的

在我的模型中我有

公共IList>ItemCategories{get;set;}

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

帮忙

我最初的想法是

//模型

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

public IList<long> DynamicCategoryItems { get; set; }
public IList动态分类{get;set;}
公共IList 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 })
}
@for(int i=0;imodel.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>              
@模型类别
@{
Entities.Category rowModel=新实体.Category();
int count=ViewBag.Index==null?0:(int)ViewBag.Index;
}
@型号.名称
@foreach(Model.CategoryItems.Where(x=>x.Enabled)中的CategoryItem项)
{
@项目名称
}
试试这个

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

}
for(int i=0;imodel.DynamicCategories[i],model=>model.DynamicCategories[i])@:Text
}

这里的文本是radiobutton的文本。

Hi,如果单选按钮位于表中并动态添加,如何确保其在表单POST上提交。我遵循了你的样本,这很有效。但是,如果我克隆一行并将其添加到表中,并生成id,它将无法为动态添加的行发布false或truevals@transformer:很抱歉回复太晚,原因是添加新控件时,其索引应按顺序排列。例如:
model.DynamicCategories[0]、model.DynamicCategories[1]
等,当您更正序列时,您将开始得到响应。热爱您的化身!顺便说一句,你能告诉我如何在动态添加/附加的行中执行此操作吗