Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig O'Mahony 364 posts 918 karma points
    Jun 30, 2015 @ 15:57
    Craig O'Mahony
    0

    Getting values of Radio Button on HttpPost

    Hi folks,

    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'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:

    Here's my content structure: enter image description here

    My Model

    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; }
        }
    }
    

    My surface controller:

    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;
            }
    
        }
    }
    

    and finally my partial:

    @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.RadioButtonFor(model => answerItem.selectedAnswer, answerItem.iAnswerID)
    
                                            <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:

    enter image description here

    But I just can't work out how to get the selections that the user has made.

    Any help would be greatly appreciated.

    Thanks, Craig

  • Casper Andersen 126 posts 508 karma points
    Jul 02, 2015 @ 14:00
    Casper Andersen
    0

    when sending over with the model as you do, you should have access to .SelectedValue and SelectedText. These should fit what you need

  • Craig O'Mahony 364 posts 918 karma points
    Jul 02, 2015 @ 14:08
    Craig O'Mahony
    0

    Nah, the models completely empty

  • nino 2 posts 71 karma points
    Jun 01, 2017 @ 20:54
    nino
    0

    Hello Im in the same situation, do you find the solution??

Please Sign in or register to post replies

Write your reply to:

Draft