MVC 4入门选择单选按钮的值 [英] MVC 4 getting values of selected radio buttons

查看:160
本文介绍了MVC 4入门选择单选按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是走出去到MVC(V4)的世界,我有真正的麻烦单选按钮列表的列表中的实际选择的值。我是从一把umbraco数据库中提取数据(但我想的什么,我试图做的是相同的原理),那里有与有答案的列表,每一个问题的问题清单。我会后,我已经在盼望迄今所做的一切,有人更聪明的比我可以点我在正确的方向:

I'm just venturing out into the world of MVC (v4) and I'm having real trouble getting the actual selected values of a list of radiobutton lists. I'm pulling the data from an Umbraco database (but I guess the principle of what I'm trying to do will be the same) where there are list of questions with each question having a list of answers. I'll post everything that I've done so far in the hope that someone more intelligent than I could point me in the right direction:

下面是我的内容结构

我的模型

namespace ICASolution.Models
{
    public class MultipleChoiceViewModel
    {
        public int iQuestionID { get; set; }
        public string zQuestionTitle { get; set; }
        public string zQuestionText { get; set; }
        public List<Answers> lAnswers { get; set; }

    }

    public class Answers
    {
        public int iAnswerID { get; set; }
        public string zAnswerText { get; set; }
        public bool bCorrect { get; set; }
        public string selectedAnswer { get; set; }
    }
}

我的面控制器:

namespace ICASolution.Controllers
{
    public class MultipleChoiceSurfaceController : SurfaceController
    {
        //
        // GET: /MultipleChoiceSurface/

        //public ActionResult Index()
        //{
        //    return PartialView("MultipleChoice", new MultipleChoiceViewModel());
        //}
        [HttpPost]
        public ActionResult Grade(MultipleChoiceViewModel model)
        {

            return RedirectToCurrentUmbracoPage();
        }

        public ActionResult Index()
        {
            var TestPage = Umbraco.Content(CurrentPage.Id);
            var questions = new List<MultipleChoiceViewModel>();

            foreach (var child in TestPage.Children)
            {
                var questionid = child.Id;
                var questiontitle = child.GetPropertyValue("questionTitle");
                var questiontext = child.GetPropertyValue("questionText");

                questions.Add(new MultipleChoiceViewModel { iQuestionID = questionid, zQuestionTitle = questiontitle, zQuestionText = questiontext, lAnswers = answerList(questionid) });

            }

            return PartialView("MultipleChoice", questions);
        }

        public List<Answers> answerList(int iMyQuestionID)
        {
            var questionPage = Umbraco.Content(iMyQuestionID);
            var answers = new List<Answers>();


            foreach(var child in questionPage.Children)
            {
                answers.Add(new Answers { iAnswerID = child.Id, zAnswerText = child.GetPropertyValue("answerTitle"), bCorrect =  child.GetPropertyValue("correctAnswer") });
            }

            return answers;
        }

    }
}

终于我的部分:

@model IEnumerable<ICASolution.Models.MultipleChoiceViewModel>


<div class="ethicsTestContainer">
    <div class="col-md-12">
        <div class="col-md-12 noRPadding">
            @using (Html.BeginUmbracoForm<ICASolution.Controllers.MultipleChoiceSurfaceController>("Grade")) { 

                    foreach (var item in Model)
                    {
                        <div class="form-group">
                            <p><strong>@item.zQuestionTitle</strong></p>
                            <p>@item.zQuestionText</p>

                            @{
                                foreach (var answerItem in item.lAnswers)
                                {
                                    <div class="radio radio-danger">
                                        @Html.RadioButton(answerItem.iAnswerID.ToString(), answerItem.iAnswerID, new { @type = "radio", @id = answerItem.iAnswerID, @name = item.iQuestionID })

                                        @*<input type="radio" name="@item.iQuestionID" id="@answerItem.iAnswerID" value="option1">*@
                                        <label for="@answerItem.iAnswerID">
                                            @answerItem.zAnswerText <span>&nbsp;</span>@answerItem.bCorrect
                                        </label>
                                    </div>
                                }
                            }
                        </div>
                    }


                <div class="col-sm-8 col-sm-push-2">
                    <button type="submit" class="btn btn-default btn-block">CLICK HERE TO COMPLETE YOUR ETHICS TEST</button>
                </div>

            }

        </div>
    </div>
</div>

在显示给用户呈现的一切罚款:

Everything renders fine when displayed to the user:

但我只是不知道如何让用户对HTTPPOST(基本上我需要计数,他们已经做出了正确答案的量)。

But I just can't work out how to get the selections that the user has made on the HTTPPOST (basically I need to count the amount of correct answers that they have made).

推荐答案

您的问题是,你正在生成一个的foreach为 MultipleChoiceViewModel 控件循环与生成复制名称属性不能被绑定到一个集合(不包括索引)和复制 ID 属性是无效的HTML。你需要生成控制在循环(或使用自定义的 EditorTemplate 类型 MultipleChoiceViewModel )

Your problem is that you are generating the controls for MultipleChoiceViewModel in a foreach loop with generates duplicate name attributes which cannot be bound to a collection (they do not include indexers) and duplicate id attributes which is invalid html. You need to generate the controls in a for loop (or use a custom EditorTemplate for type of MultipleChoiceViewModel)

您还需要将 selectedAnswer 财产移至 MultipleChoiceViewModel (不是 selectedAnswer

You also need to move the selectedAnswer property to MultipleChoiceViewModel (not in selectedAnswer)

使用循环(模型必须的IList&LT; MultipleChoiceViewModel&GT;

for(int i = 0; i < Model.Count; i++)
{
  @Html.HiddenFor(m => m[i].iQuestionID) // for post back
  @Html.DisplayFor(m => m[i].zQuestionTitle)
  ... 
  foreach(var item in Model[i].lAnswers)
  {
    @Html.RadioButtonFor(m => m[i].selectedAnswer, item.iAnswerID, new { id = item.iAnswerID })
    <label for="@item.iAnswerID">@item.zAnswerText</label>
  }
}

要使用 EditorTempate ,创建的局部视图/查看/共享/ EditorTemplates 名为 MultipleChoiceViewModel.cshtml

@model yourAssembly.MultipleChoiceViewModel
@Html.HiddenFor(m => m.iQuestionID)
@Html.DisplayFor(m => m.zQuestionTitle)
... 
foreach(var item in Model.lAnswers)
{
  @Html.RadioButtonFor(m => m.selectedAnswer, item.iAnswerID, new { id = item.iAnswerID })
  <label for="@item.iAnswerID">@item.zAnswerText</label>
}

,然后在主视图,以

and then in the main view,replace the for loop with

@Html.EditorFor(m => m)

EditorFor()方法将基于模板的集合中的每个项目生成HTML。

The EditorFor() method will generate the html based on the template for each item in the collection.

旁注: RadioButtonFor()生成类型=电台所以加入<$ C $一个HTML C> @type =电台是没有意义的。而使用HTML佣工时,切勿覆盖名称属性

Side notes: RadioButtonFor() generates type="radio" so adding a html for @type = "radio" is pointless. And NEVER override the name attribute when using html helpers

这篇关于MVC 4入门选择单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆