ASP.Net MVC 提交调查的帖子数据
当为收集调查答案的表单提交时,我很难弄清楚如何从 FormCollection 中收集数据。具体来说,我的问题是具有多个选择选项(单选按钮)和其他文本框字段(如果选项不适用)的问题。
我的调查具有以下结构:
问题:[ QuestionId、Text、QuestionType、OrderIndex]
MULTIPLE_CHOICE_OPTIONS:[ MC_OptionId、QuestionId、OrderIndex、MC_Text]
ANSWER:[AnswerId、QuestionId、MC_OptionId(可以为空)、UserTextAnswer]
QUESTION_TYPES 为:[Multiple_Choice ,Multiple_Choice_wOtherOption,FreeText 或 Checkbox]
我的视图正在渲染表单,如下所示(伪代码以简化):
//Html.BeginForm
foreach( Question q in Model.Questions)
{
q.Text //display question text in html
if (q.QuestionType == Multiple_Choice)
{
foreach( MultipleChoice_Option mc in Model.MULTIPLE_CHOICE_OPTIONS(opt => opt.QuestionId == q.QuestionId)
{
<radio name=q.QuestionId value=mc.MC_OptionId />
// All OK, can use the FormCollectionKey to get the
// QuestionId and its value to get the selected MCOptionId
}
}
else if (q.QuestionType == Multiple_Choice_wOtherOption)
{
foreach( MultipleChoice_Option mc in Model.MULTIPLE_CHOICE_OPTIONS(opt => opt.QuestionId == q.QuestionId)
{
<radio name=q.QuestionId value=mc.MC_OptionId />
}
<textbox name=q.QuestionId />
// ****Problem - I can get the QuestionId from the FormCollection Key, but
// I don't know if the value is from the user entered
// textbox or from a MCOptionId***
}
}
<button type="submit">Submit Survey</button>
// Html.EndForm
我就是这样做的,所以回到处理回发的控制器操作中,我可以通过键读取 FormCollection 以获取QuestionId,以及用于获取 MCOptionID 的每个索引的值。 但是,对于单选按钮和文本框均具有相同名称键的问题,我如何确定表单数据是来自单选按钮还是文本框。
我可以看到我这样做的方式中断,因为它们可能是这样的情况:问题 (id=1) 具有 MCOption w/ Id=5,因此单选按钮的值为 5,并且用户在“其他”文本框中输入 5 。当表单提交时,我看到 formcollection[key="1"] 的值为 5,但我无法判断该值是来自用户文本还是引用 MCOptionId 的 radioButton 值。
有没有更好的方法来解决这个问题,无论是数据库结构、视图渲染代码还是表单控件的命名方式?也许表单收集不是正确的方法,但我很困惑如何回发并使模型绑定工作。
感谢您的帮助,一直在绕圈寻找一些看起来很简单的事情。
I'm having a hard time figuring out how to collect the data from a FormCollection when submitted for my form which collects the answers for a survey. Specifically my problem is with questions that have multiple choice options(radio buttons) and an other textbox field if the options did not apply.
My survey has the following structure:
QUESTION : [ QuestionId, Text, QuestionType, OrderIndex]
MULTIPLE_CHOICE_OPTIONS: [ MC_OptionId, QuestionId, OrderIndex, MC_Text]
ANSWER: [AnswerId,QuestionId, MC_OptionId (can be null), UserTextAnswer]
QUESTION_TYPES are: [Multiple_Choice, Multiple_Choice_wOtherOption, FreeText or Checkbox]
My view is rendering the form as follows(pseudo code to simplify):
//Html.BeginForm
foreach( Question q in Model.Questions)
{
q.Text //display question text in html
if (q.QuestionType == Multiple_Choice)
{
foreach( MultipleChoice_Option mc in Model.MULTIPLE_CHOICE_OPTIONS(opt => opt.QuestionId == q.QuestionId)
{
<radio name=q.QuestionId value=mc.MC_OptionId />
// All OK, can use the FormCollectionKey to get the
// QuestionId and its value to get the selected MCOptionId
}
}
else if (q.QuestionType == Multiple_Choice_wOtherOption)
{
foreach( MultipleChoice_Option mc in Model.MULTIPLE_CHOICE_OPTIONS(opt => opt.QuestionId == q.QuestionId)
{
<radio name=q.QuestionId value=mc.MC_OptionId />
}
<textbox name=q.QuestionId />
// ****Problem - I can get the QuestionId from the FormCollection Key, but
// I don't know if the value is from the user entered
// textbox or from a MCOptionId***
}
}
<button type="submit">Submit Survey</button>
// Html.EndForm
I was doing it this way so back in the controller action that handles the post back I could read over the FormCollection by key to get the questionId, and the value for each index to get the MCOptionID.
But in the case of the question with radio buttons and a textbox all with the same name key how would I determine if the form data is from the radio button or the text box.
I can see the way I'm doing this breaks because their could be the case where a question (id=1) has a MCOption w/ Id=5 so the radio button has value of 5 and the user enters 5 in the Other textbox. When the form submits I see the formcollection[key="1"] has the value 5 and I can't tell if that is from usertext or the radioButton value referencing a MCOptionId.
Is there a better way to approach this problem, either the db structure, view rendering code or the way the form controls are named? Maybe form collection is not the way to go but I was stumped how to post back and make the model binding work .
Thanks for any help, been going around in circles for something that seems pretty simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑这个小的重构......
在强类型的部分视图中:
看起来你的问题是围绕文本框名称;通过 ID 与问题相关联。在控制器中,您必须明确知道何时在文本框中查找任何值。
Consider this small refactoring...
and within that strongly typed partial view:
It seems your question was around the textbox name; being tied to the question by ID. In your controller, you'll have to explicitly know when to look for any value in the textbox.