简体   繁体   English

ASP.NET MVC多项选择调查

[英]ASP.NET MVC Multiple choice survey

I'm creating a simple survey using ASP.NET MVC. 我正在使用ASP.NET MVC创建一个简单的调查。 The administrator will create questions answerable by 3 options only - Strongly agree , somewhat agree , strongly disagree ) via the Create View. 管理员将仅通过3个选项来创建可回答的问题- 完全同意有些同意非常不同意 )(通过创建视图)。

The user will then have to select their answer using radiobutton. 然后,用户将不得不使用单选按钮选择答案。

  1. How should I add it in my view? 我应该如何添加它?

Currently, this is how my view looks like (I know this is still wrong as im still trying to understand how to do this correctly): 目前,这就是我的观点(我知道这仍然是错误的,因为即时消息仍在尝试理解如何正确执行此操作):

<table class="table">
    <tr>
        <th>
            Question
        </th>
        <th class="text-center">
            Strongly Agree
        </th>
        <th class="text-center">
            Somewhat Agree
        </th>
        <th class="text-center">
            Strongly Disagree
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Question)
            </td>
            <td align="center">
                @Html.RadioButtonFor(modelItem => item.Answer, "Strongly Agree", new { QuestionID = item.QuestionID })
            </td>
            <td align="center">
                @Html.RadioButtonFor(modelItem => item.Answer, "Somewhat Agree", new { QuestionID = item.QuestionID })
            </td>
            <td align="center">
                @Html.RadioButtonFor(modelItem => item.Answer, "Strongly Disagree", new { QuestionID = item.QuestionID })
            </td>
        </tr>
    }

</table>
  1. How would I get the selected answer of the user for each question and save it to my database? 如何获得每个问题的用户所选答案并将其保存到我的数据库中?

My tables are as follows: 我的表格如下:

tblTestProper: [ QuestionID , UserID , Answer ] tblTestProper: [ QuestionIDUserIDAnswer ]

tblQuestions: [ QuestionID , Question ] tblQuestions: [ QuestionIDQuestion ]

Are there any examples or ideas I can follow to achieve this? 我可以遵循任何示例或想法来实现这一目标吗? Thanks for any help. 谢谢你的帮助。

 <table class="table">
                <tr>
                    <th></th>
                    <th></th>
                    <th>Question</th>
                    <th></th>
                    <th></th>
                    <th class="text-center">Strongly Agree</th>
                    <th class="text-center">Somewhat Agree</th>
                    <th class="text-center">Strongly Disagree</th>
                </tr>

                @for (int i = 0; i < Model.Count(); i++)
                {
                    <tr>   
                        <td>@Html.HiddenFor(m => m[i].AnswerID)</td>
                        <td>@Html.HiddenFor(m => m[i].QuestionID)</td>
                        <td>@Html.DisplayFor(m => m[i].Question)</td>
                        <td>@Html.HiddenFor(m => m[i].QuestionTag)</td>
                        <td align="right">@Html.ValidationMessageFor(m => m[i].Answer, "", new { @class = "text-danger" })</td>
                        <td align="center">@Html.RadioButtonFor(m => m[i].Answer, 1)</td>
                        <td align="center">@Html.RadioButtonFor(m => m[i].Answer, 2)</td>
                        <td align="center">@Html.RadioButtonFor(m => m[i].Answer, 3)</td>
                    </tr>
                }
            </table>

I think hope you helpful this 我想希望您对此有所帮助

html code like this 像这样的html代码

    <label class="form-radio form-normal active form-text">1<input type="radio" class="rdomcqans" name="qug1" value="1"></label>
    <label class="form-radio form-normal active form-text">2<input type="radio" class="rdomcqans" name="qug1" value="2"></label>
    <label class="form-radio form-normal active form-text">3<input type="radio" class="rdomcqans" name="qug1" value="3"></label>
    <label class="form-radio form-normal active form-text">4<input type="radio" class="rdomcqans" name="qug1" value="4"></label>
    <!--qug1 = Question Group 1-->
    </br>
    <label class="form-radio form-normal active form-text">1<input type="radio" class="rdomcqans" name="qug2" value="1"></label>
    <label class="form-radio form-normal active form-text">2<input type="radio" class="rdomcqans" name="qug2" value="2"></label>
    <label class="form-radio form-normal active form-text">3<input type="radio" class="rdomcqans" name="qug2" value="3"></label>
    <label class="form-radio form-normal active form-text">4<input type="radio" class="rdomcqans" name="qug2" value="4"></label>
    <!--qug2 = Question Group 2-->
    <input type="hidden" id="McqAnswerListArray" name="McqAnswerListArray" />

    <script>
        $('.rdomcqans').on('change', function () {
            var McqAnswerListArray="";
            var count = 0;
            McqAnswerListArray += "";
            $('.rdomcqans:checked').each(function () {

                if (count == 0) {            
                    McqAnswerListArray += $(this).attr('name')+ ":" + $(this).val();
                } else {
                    McqAnswerListArray += ",";
                    McqAnswerListArray += $(this).attr('name') + ":" + $(this).val();
                }

                count++;
            });

            $('#McqAnswerListArray').val(McqAnswerListArray);
        });
    </script>

Controller Should be like this 控制器应该是这样的

You should be import using Newtonsoft.Json; 您应该使用Newtonsoft.Json导入

[HttpPost]
public ActionResult MCQ(String McqAnswerListArray) {
    string[] Jsonn = mcm.McqAnswerListArray.Split(',');

    for (int i = 0; i < Jsonn.Count(); i++) {
        string s = Jsonn[i];
        string[] obj = s.Split(':');
        string strqid = obj[0].ToString();
        int qid = Convert.ToInt32(strqid.Substring(3, strqid.Length - 3));//Get Question Group Number
        int Answer = Convert.ToInt32(obj[1].ToString());//Get Group Answer
        //You Can save Your answer bellow
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM