如何在 ASP.net Core / 5 中建模绑定动态单选按钮

问题描述

我有一个 Likert 调查生成器,可以在其中输入动态数量的问题 - 然后用户通过 4 个单选按钮(非常不同意、不同意、同意、非常同意)浏览并回答每个问题。

我可以很容易地输出它们 - 但是它们可以在回帖中进行模型绑定还是我必须遍历已发布的表单元素(即不使用模型绑定)?

我在谷歌上搜索了很多,但没有找到解决方案 - 可能吗?

谢谢。

解决方法

如果你想在视图中绑定一个列表,这里有一个关于绑定一个带有输入名称的列表的演示:

型号:

public class Qusetion 
    {
        public string Content { get; set; }
        public string Answer { get; set; }

    }

查看:

<form method="post">
    <div>
        <label>question1</label>
        <input name="list[0].Content" value="question1" hidden/>
        <div>
            <input type="radio" name="list[0].Answer" value="Strongly Disagree" />Strongly Disagree
            <input type="radio" name="list[0].Answer" value="Disagree" />Disagree
            <input type="radio" name="list[0].Answer" value="Agree" />Agree
            <input type="radio" name="list[0].Answer" value="Strongly Agree" />Strongly Agree
        </div>
    </div>
    <div>
        <label>question2</label>
        <input name="list[1].Content" value="question2" hidden/>
        <div>
            <input type="radio" name="list[1].Answer" value="Strongly Disagree" />Strongly Disagree
            <input type="radio" name="list[1].Answer" value="Disagree" />Disagree
            <input type="radio" name="list[1].Answer" value="Agree" />Agree
            <input type="radio" name="list[1].Answer" value="Strongly Agree" />Strongly Agree
        </div>
    </div>
    <div>
        <label>question3</label>
        <input name="list[2].Content" value="question3" hidden/>
        <div>
            <input type="radio" name="list[2].Answer" value="Strongly Disagree" />Strongly Disagree
            <input type="radio" name="list[2].Answer" value="Disagree" />Disagree
            <input type="radio" name="list[2].Answer" value="Agree" />Agree
            <input type="radio" name="list[2].Answer" value="Strongly Agree" />Strongly Agree
        </div>
    </div>
    <input type="submit" value="submit" />
</form>

控制器:

public IActionResult BindList(List<Qusetion> list)
        {
           
            return View();
        }

结果: enter image description here

更新:

如果你想用循环绑定。你可以把列表传递给视图。代码会起作用是因为.net核心绑定了具有名称属性的模型。而且因为你想绑定列表,所以名字会像{{ 1}}。 这是一个带有循环的演示:

查看:

list[index].xxx

控制器:

@model IEnumerable<Qusetion>
<form method="post">
    @{ var i = 0;}
    @foreach (var question in Model)
    {
        <div>
            <label>@question.Content</label>
            <input name="list[@i].Content" value="@question.Content" hidden />
            <div>
                <input type="radio" name="list[@i].Answer" value="Strongly Disagree" />Strongly Disagree
                <input type="radio" name="list[@i].Answer" value="Disagree" />Disagree
                <input type="radio" name="list[@i].Answer" value="Agree" />Agree
                <input type="radio" name="list[@i].Answer" value="Strongly Agree" />Strongly Agree
            </div>
        </div>
        i++;
    }
    
    <input type="submit" value="submit" />
</form>

结果: enter image description here