Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建多个相同单选按钮组_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 创建多个相同单选按钮组

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

我有一个模型,其中有一个内部类TripSeatReference的列表,我想为该列表中的每个项创建一组新的单选按钮。我在下面的视图中写了我的尝试,它现在不起作用。我的索引似乎超出了for循环的范围

型号:

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++)