0

I am having a slight issue sending checked grouped radio data to my controller action method in MVC

My View

@model IDictionary<int, List<ElectronicVotingSystem.ViewModels.VoteViewModel>>
@using (Html.BeginForm("ProcessVote", "Voter", FormMethod.Post, new { Id = "VoteForm" }))
            {
                foreach (var group in Model)
                {
                    var name = group.Value.Where(m => m.Precedence == group.Key).Select(m => m.PositionName).FirstOrDefault();
                <fieldset style="padding: 0 10px 0 10px" class="text-center">
                    <legend id="head">@name</legend>
                    <div class="row">
                        @foreach (var item in group.Value)
                        {
                            <div class="col-sm-4" style="margin-left: 50px">
                                <div class="row">
                                    <div class="col-sm-12">
                                        <img src="~/Aspirant/RetrieveImage/@item.Id" alt="Passport Picture" height=200 width=300 />
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-sm-12 text-center">
                                        <span style="font-weight: bold; font-size: 18px" id="applicant-name">@item.FirstName @item.MiddleName @item.Surname</span>
                                    </div>
                                    <div class="col-sm-12">
                                        @Html.RadioButton(item.PositionName, item.Id)
                                    </div>
                                </div>
                            </div>
                        }
                    </div>
                </fieldset>
                }
                <button id="SubmitForm" type="submit" class="btn btn-primary" style="margin-right: 35%">Submit form</button>
            }

My Controller

[HttpPost]
    public ActionResult ProcessVote(IDictionary<int, List<VoteViewModel>> voteViewModel)
    {
        return View();
    }

When I post to the action, I get Specified cast is not valid. error. Can someone help me on how to post values of the checked radio button groups to the controller? Thanks in advance.

  • You cannot use `foreach` loops to generate form controls for collections (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) and MVC does not work well with dictionaries (refer [this answer](http://stackoverflow.com/questions/5191303/asp-net-mvc-binding-to-a-dictionary) for how it canbe done). –  Sep 05 '16 at 23:47
  • Very little in your code makes sense, but start by creating a view model containing properties (say) `int Key` and `List Votes` and pass a collection of that model to the view, then use nested `for` loops of `EditorTemplate`'s to generate the view so you strongly bind to your model –  Sep 05 '16 at 23:48
  • But its unclear what your trying to so. Your creating a series of radio buttons that don't relate to anything and would be meaningless when you submit the form –  Sep 05 '16 at 23:51

0 Answers0