C# 创建多个相同单选按钮组
我有一个模型,其中有一个内部类TripSeatReference的列表,我想为该列表中的每个项创建一组新的单选按钮。我在下面的视图中写了我的尝试,它现在不起作用。我的索引似乎超出了for循环的范围 型号:C# 创建多个相同单选按钮组,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个模型,其中有一个内部类TripSeatReference的列表,我想为该列表中的每个项创建一组新的单选按钮。我在下面的视图中写了我的尝试,它现在不起作用。我的索引似乎超出了for循环的范围 型号: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using MVC_COMP156
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using MVC_COMP1562.Models;
namespace MVC_COMP1562.ViewModels
{
public class SeatPreferencesShoppingCartViewModel
{
public ShoppingCart ShoppingCart { get; set; }
public float Total { get; set; }
public List<tripSeatPreference> Preferences { get; set; }
public IEnumerable<string> SeatPreferenceOptions { get; set; }
public class tripSeatPreference
{
public int Id { get; set; }
[Required]
public int? SeatPreferenceSelected { get; set; }
public CartItem CartItem { get; set; }
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
使用MVC_COMP1562.0模型;
名称空间MVC_COMP1562.ViewModels
{
公共类SeatReferenceShoppingCartViewModel
{
公共购物车购物车{get;set;}
公共浮点总数{get;set;}
公共列表首选项{get;set;}
公共IEnumerable SeatReferenceOptions{get;set;}
公共类TripSeatReference
{
公共int Id{get;set;}
[必需]
public int?SeatPreferenceSelected{get;set;}
公共CartItem CartItem{get;set;}
}
}
}
视图:
@model MVC\u COMP1562.ViewModels.seatpreferenceshoppingcartviewmodel
@对于(int i=0;iModel.Preferences[i].CartItem.Trip.Title)
foreach(型号中的var无线电。座椅参考选项)
{
@Html.RadioButton(m=>Model.Preferences[i].SeatPreferencesSelected,Model.Preferences[i].Id,新的{Id=Model.Preferences[i].Id})
@无线电
}
}
您正在SeatPreferenceOptions
上循环,而在循环内部,您正试图使用SeatPreferenceOptions
集合的索引从SeatPreferenceOptions
获取项目,这显然会失败,因为其中一个选项中可能有或多或少的项目,所以请将循环更改为:
@for(int i=0;i<Model.SeatPreferenceOptions.Count();i++)
@for(int i=0;i
@for(int i=0;i<Model.SeatPreferenceOptions.Count();i++)
@for(int i=0;i<Model.SeatPreferences.Count();i++)