ASP.Net MVC 提交调查的帖子数据

发布于 2024-09-18 08:33:07 字数 1991 浏览 1 评论 0原文

当为收集调查答案的表单提交时,我很难弄清楚如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-09-25 08:33:07

考虑这个小的重构......

//you're always rendering the radios, it seems?
RenderPartial("MultipleChoice", Model.MULTIPLE_CHOICE_OPTIONS.Where(x => 
                                   x.QuestionId == q.QuestionId));

if (q.QuestionType == Multiple_Choice_wOtherOption)
{   
   <textbox name="Other|" + q.QuestionId />      
}

在强类型的部分视图中:

//Model is IEnumerable<MultipleChoice_Option >
foreach (MultipleChoice_Option mc in Model ) 
{
    <radio name=mc.Question.QuestionId value=mc.MC_OptionId />           
}

看起来你的问题是围绕文本框名称;通过 ID 与问题相关联。在控制器中,您必须明确知道何时在文本框中查找任何值。

 string userAnswer = Request.Form["OtherEntry|" + someQuestionID].ToString();

Consider this small refactoring...

//you're always rendering the radios, it seems?
RenderPartial("MultipleChoice", Model.MULTIPLE_CHOICE_OPTIONS.Where(x => 
                                   x.QuestionId == q.QuestionId));

if (q.QuestionType == Multiple_Choice_wOtherOption)
{   
   <textbox name="Other|" + q.QuestionId />      
}

and within that strongly typed partial view:

//Model is IEnumerable<MultipleChoice_Option >
foreach (MultipleChoice_Option mc in Model ) 
{
    <radio name=mc.Question.QuestionId value=mc.MC_OptionId />           
}

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.

 string userAnswer = Request.Form["OtherEntry|" + someQuestionID].ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文