bind< string,string>词典到复选框MVC(制作复选框返回srtring值)
我在ViewModel
public Dictionary<string, string> Answers { get; set; }
public List<KeyValuePair<Question, List<string>>> QuestionsAndOptions { get; set; }
public TestVM()
{
Answers = new Dictionary<string, string>();
QuestionsAndOptions = new List<KeyValuePair<Question, List<string>>>();
}
QuestionandOptions
中包含问题
及其对我的测试页面的选项IM传递到查看的选项。 我的项目中有不同的问题类型,其中有多个答案选项是其中之一。
答案
包含&lt; string,string&gt;问题ID和用户答案对
我的视图中我得到了此代码:
@for (int i = 0; i < Model.QuestionsAndOptions.Count; i++)
{
var question = Model.QuestionsAndOptions[i];
<tr>
<td>
<b>@question.Key.Text</b>
@if (question.Key.TypeId == 1) /*single, radiobuttons*/
{
foreach (var answer in question.Value)
{
<input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
<input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
<input type="hidden" name="testId" value="@Model.Test.Id" />
@Html.RadioButton("testVM.Answers[" + question.Key.Id + "].Value", answer, false) @answer
}
}
@if (question.Key.TypeId == 2) /*multiple, checkboxes*/
{
foreach (var answer in question.Value)
{
<input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
<input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
<input type="hidden" name="testId" value="@Model.Test.Id" />
@Html.CheckBox("testVM.Answers[" + question.Key.Id + "].Value", answer) @answer
}
}
@if (question.Key.TypeId == 3) /*string text, textbox*/
{
foreach (var answer in question.Value)
{
<input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
<input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
<input type="hidden" name="testId" value="@Model.Test.Id" />
@Html.TextBox("testVM.Answers[" + question.Key.Id + "].Value")
}
}
第一和第三,如果(){...}正常工作,我在提交后得到了我需要的东西,但是对于复选框,我显然是正确的/false
我希望复选框通过字符串(检查值)来回答字典
I got this structure in my ViewModel
public Dictionary<string, string> Answers { get; set; }
public List<KeyValuePair<Question, List<string>>> QuestionsAndOptions { get; set; }
public TestVM()
{
Answers = new Dictionary<string, string>();
QuestionsAndOptions = new List<KeyValuePair<Question, List<string>>>();
}
QuestionsAndOptions
contains Questions
and its options for my Test Page im passing to VIew.
I got different question types in my project and questions with multiple answer options is one of them.
Answers
contains <string,string> pairs of question IDs and user Answer for it
I got this code in my View:
@for (int i = 0; i < Model.QuestionsAndOptions.Count; i++)
{
var question = Model.QuestionsAndOptions[i];
<tr>
<td>
<b>@question.Key.Text</b>
@if (question.Key.TypeId == 1) /*single, radiobuttons*/
{
foreach (var answer in question.Value)
{
<input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
<input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
<input type="hidden" name="testId" value="@Model.Test.Id" />
@Html.RadioButton("testVM.Answers[" + question.Key.Id + "].Value", answer, false) @answer
}
}
@if (question.Key.TypeId == 2) /*multiple, checkboxes*/
{
foreach (var answer in question.Value)
{
<input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
<input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
<input type="hidden" name="testId" value="@Model.Test.Id" />
@Html.CheckBox("testVM.Answers[" + question.Key.Id + "].Value", answer) @answer
}
}
@if (question.Key.TypeId == 3) /*string text, textbox*/
{
foreach (var answer in question.Value)
{
<input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
<input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
<input type="hidden" name="testId" value="@Model.Test.Id" />
@Html.TextBox("testVM.Answers[" + question.Key.Id + "].Value")
}
}
First and third If(){...} works fine and i getting what i need after submit, but for checkboxes i'm obviously geting true/false
I want checkboxes to pass strings (checked values) to Answer dictionary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要将值绑定到复选框,则可以尝试在复选框中设置
value
,尝试使用以下代码:选中复选框时复选框的值(
@answer )将传递给行动。
If you want to bind value to checkbox,you can try to set
value
in checkbox,try to use the following code:When the checkbox is checked the value of the checkbox(
@answer
) will be passed to action.