Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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# 发送列表以在asp.net mvc中查看、填写和获取_C#_Asp.net Mvc_Radio Button - Fatal编程技术网

C# 发送列表以在asp.net mvc中查看、填写和获取

C# 发送列表以在asp.net mvc中查看、填写和获取,c#,asp.net-mvc,radio-button,C#,Asp.net Mvc,Radio Button,我有一个测验表格,它显示了一些问题及其答案(单选按钮), 用户必须回答问题并发送, 我上过这样的课: public class Question { public int Qid { get; set; } public string Questionstext { get; set; } public int selected { get; set; } public List<Qitem> lst { get; set; } } publ

我有一个测验表格,它显示了一些问题及其答案(单选按钮), 用户必须回答问题并发送, 我上过这样的课:

  public class Question 
{

    public int Qid { get; set; }
    public string Questionstext { get; set; }
    public int selected { get; set; }
    public List<Qitem> lst { get; set; }

}
public class Qitem
{
    public int id { get; set; }
    public string txt { get; set; }

}
公开课问题
{
公共int Qid{get;set;}
公共字符串问题text{get;set;}
选定的公共int{get;set;}
公共列表lst{get;set;}
}
公开课
{
公共int id{get;set;}
公共字符串txt{get;set;}
}
然后我将问题列表发送到视图中:

 List<Question> llst = new List<Question>
        {
            new Question
            {
                Qid = 1,
                Questionstext = "is it true?",
                lst = new List<Qitem>
                {
                    new Qitem
                    {
                        txt = "yes",
                        id = 1
                    },
                    new Qitem
                    {
                        id = 2,
                        txt = "no"
                    }
                }
            },
            new Question
            {
                Qid = 1,
                Questionstext = "is it true 2?",
                lst = new List<Qitem>
                {
                    new Qitem
                    {
                        txt = "yes2",
                        id = 3
                    },
                    new Qitem
                    {
                        id = 4,
                        txt = "yes3"
                    }
                }
            }
        };

        return View(llst);
List llst=新列表
{
新问题
{
Qid=1,
问题text=“这是真的吗?”,
lst=新列表
{
新Qitem
{
txt=“是”,
id=1
},
新Qitem
{
id=2,
txt=“否”
}
}
},
新问题
{
Qid=1,
问题text=“这是真的吗?”,
lst=新列表
{
新Qitem
{
txt=“yes2”,
id=3
},
新Qitem
{
id=4,
txt=“是的”
}
}
}
};
返回视图(llst);
我如何展示它,然后提交答案, 答案必须放在问题的“选定”属性中。 我的问题是关于视图,特别是单选按钮。

您的视图应该是

@model List<Question>
@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        @Html.HiddenFor(m => m[i].Qid)
        <h2>@Model[i].Questionstext</h2>
        foreach (var answer in Model[i].lst)
        {
            <label>
                @Html.RadioButtonFor(m => m[i].selected, answer.id, new { id = "" })
                <span>@answer.txt </span>
            </label>
        } 
    }
    <input type="submit" value="Save" />
}

旁注:如果您还希望发布
Questionstext
值,请包括
@Html.HiddenFor(m=>m[i].Questionstext)

我们还可以使用Viewbag或tempdata来存储列表,并在将不同型号的多个列表发送到服务器时在视图中使用view@HimaanSingh,
TempData
不适合将数据发送到视图,建议在强类型模型上使用
ViewBag
,而OP正在做的是非常实用的
public ActionResult Index(List<Question> model)