In my MVC Project, I currently have the following code in my view

EDIT: I just checked and the @using statement can be replaced by a simple <form> and it still seems to all work the same. Still having the same problem however.

    @using (Html.BeginForm("Index", "AccountsFinance", FormMethod.Post))
    {
        <table cellspacing="10">
            <tr>
                @foreach (string answer in Model.Answers)
                {
                <td><input type="radio" name="answer" value="@answer" /><span>@answer</span></td>
                }
            </tr>
        </table>
        <div id="footer">
            <input class="btn btn-success" direction="false" type="submit" name="Back" value="Back" />
            @if(Model.NextQuestion == 6)
            {
                <input type="submit" @Html.ActionLink("Next", "Index", "Result") />
            }
            else
            {
                <a id="nextButton">@Html.ActionLink("Next", "Index", new { questionId = Model.NextQuestion })</a>
            }
        </div>
    }

What I need is for when the user clicks on the @Html.ActionLink... (which has the id="nextButton), the form sends to the controller method, the value of the selected radio buttons. Here is the code in my Controller:

    public ActionResult Index(string questionId)
    {
        int qId = int.Parse(questionId);

        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == qId).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == qId).Select(s => s.AnsAnswer).ToList();
            afqList.NextQuestion = qId + 1;
        }

        return View("Index", afqList);
    }

I have achieved this before, but not when using the @Html.BeginForm elements etc. So how do I pass back the value of the selected radio button to the controller, when the @Html.ActionLink("Next"... is clicked? Thanks in advance!


EDIT 2: Another similar question which may also solve my problem, how would I assign the value of the selected radio button to a variable created in the model, similar to questionId = Model.NextQuestion. If this question was answered, I could simply do the same as I have done with Model.NextQuestion with the selected radio button value.

Related posts

Recent Viewed