0

我有这个代码片段,我正在编写一个 ASP.NET MVC 应用程序。下面是 HTML 和后面的代码。我需要有关如何一次只允许为每个问题选择一个单选按钮以及如何保存用户响应的帮助。

看法

@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{ 
    <div class="jumbotron">
        @for (int i = 0; i < Model.Count(); i++)
        {
            var inputTypes = Model[i].InputTypes.Split(',').ToList();

            <p>@Model[i].Details</p>

            for (int j = 0; j < Model[i].Options.Count; j++)
            {
                for (int x = 0; x < inputTypes.Count; x++)
                {
                    if (inputTypes[x] == "TextBox")
                    {
                        @Html.TextBoxFor(u => Model[i].Options[j].Details, new { @class = "form" })
                    }
                    else if (inputTypes[x] == "CheckBox")
                    {
                        @Html.CheckBoxFor(u => Model[i].Options[j].Details, new { @class = "form" })
                    }
                    else if (inputTypes[x] == "RadioButton")
                    {
                        @Html.RadioButtonFor(u => Model[i].Options[j].Details, new { @class = "form" })
                    }
                }
            }
        }
    </div>
}

控制器

public ActionResult Index()
{
    var questions = _context.Questions.ToList();

    var questionModel = new List<Question>();

    questions.ForEach(q =>
    {
        var model = new Question
        {
            Id = q.Id,
            Title = q.Title,
            Details = q.Details,
            HasMultiAnswers = q.HasMultiAnswers,
            NumberOfAnswers = q.NumberOfAnswers,
            InputTypes = q.InputTypes, //New Line 
            Options = _context.Options.Where(f => f.point == 10).ToList()
        };
        questionModel.Add(model);
    });
    return View(questionModel);
}

模型

    public class Question
    {
    public long Id { get; set; }
    public string Title { get; set; }
    public string Details { get; set; } 
    public bool HasMultiAnswers { get; set; }
    public int NumberOfAnswers { get; set; }
    public double TotalPoint { get; set; } 
    public String InputTypes { get; set; } 
    public System.Collections.Generic.List<Option> Options { get; set; }
  }

  public class Option
  {
    public int Id { get; set; }
    public int Order { get; set; }
    public string Details { get; set; } 
    public double point { get; set; } 
  }

更新:模型已针对代码进行了更新。

--- 回复后,我更新了代码

    @model List<Demo.Controllers.HomeController.MModel>

@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
    <div class="jumbotron">
        @for (int i = 0; i < Model.Count(); i++)
        {
            var inputTypes = Model[i].InputTypes.Split(',').ToList();
            <p> @Model[i].Details</p>

            for (int j = 0; j < Model[i].Options.Count; j++)
            {
                for (int x = 0; x < inputTypes.Count; x++)
                {
                    if (inputTypes[x] == "TextBox")
                    {
                        @Html.TextBoxFor(u => Model[i].Options[j].Name, new { @class = "form-control" })
                        <br />
                    }
                    else if (inputTypes[x] == "CheckBox")
                    {
                        @Html.CheckBoxFor(u => Model[i].Options[j].IsBoolean.Value, new { @class = "form-control", @checked = "false" })
                        <br />
                    }
                    else if (inputTypes[x] == "RadioButton")
                    {
                        @Html.RadioButtonFor(u => Model[i].Options[j].IsBoolean, "false", new { @class = "form-control" })
                        <br />
                    }
                    else if (inputTypes[x] == "Multiple")
                    {
                        @Html.CheckBoxFor(u => Model[i].Options[j].IsBoolean.Value, new { @class = "form-control", @value = "false" })
                        <br />
                        @Html.TextBoxFor(u => Model[i].Options[j].Name, new { @class = "form-control" })
                        <br />
                    }
                }
            }
        }
    </div>
}


public ActionResult Index()
    {
        var questions = _context.aQuestions.ToList();

        var questionModel = new List<MModel>();

        questions.ForEach(q =>
        {
            var model = new MModel
            {
                QuestionID = q.QuestionID,
                Title = q.Title,
                Details = q.Details,
                HasMultiAnswers = q.HasMultiAnswers,
                NumberOfAnswers = q.NumberOfAnswers,
                InputTypes = q.InputTypes, //New Line 
                Options = _context.aOptions.Where(f => f.QuestionID == q.QuestionID).ToList()
            };
            questionModel.Add(model);
        });
        return View(questionModel);
    }
    public class MModel
    {
        public short QuestionID { get; set; }
        public string Title { get; set; }
        public string Details { get; set; }
        public Nullable<bool> HasMultiAnswers { get; set; }
        public Nullable<byte> NumberOfAnswers { get; set; }
        public string InputTypes { get; set; }
        public System.Collections.Generic.List<aOption> Options { get; set; }
    }
4

1 回答 1

0

单选按钮通常用于从多值列表中返回选择。要只允许选择一个单选按钮,您必须将单选按钮组绑定到单个模型属性。例子:

@Html.RadioButtonFor(x => x.Answer, new { value = "a" })
@Html.RadioButtonFor(x => x.Answer, new { value = "b" })
@Html.RadioButtonFor(x => x.Answer, new { value = "c" })

其中 x 表示Answer在此示例中具有属性的模型。现在根据用户选择,Answer值将设置为 a、b 或 c

于 2018-10-30T23:19:47.970 回答