I have this code snippet here that works, I am writing an ASP.NET MVC application. below is the HTML and the Code behind. I need help on how to allow only one radio button to be selected at a time for each question and secondly how to save the User response.
View
@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>
}
Controller
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);
}
MODEL
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; }
}
UPDATE: The Model has been Updated For the Code.
--- After Responses, I have updated the Code
@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; }
}