0

I am trying to create radio button groups that correspond to a "registration". Each registration can have multiple "payment options" I need to be able to select an option for each registration then pass those values to the controller. I have looked at the solutions and the comments from yesterday and this is my updated code but its still not working.

<div id="regsAndFees">
    <ol>
        @foreach (var reg in Model.PendingRegistrations.Cart)
        {
            <li>
                <label>@reg.Description - </label> <span><label>@reg.Fee.ToString("C")</label></span>
                <ul style="list-style:none;">
                    @foreach (var fee in reg.AdditionalFees)
                    {
                        <li>
                            <label>@fee.AdditionalFee.ToString("C")</label>
                            <span>@fee.Description</span>
                        </li>
                    }
                </ul>
                @for (int i = 0; i < Model.PaymentOptions.RegistrationPaymentTypes.Count; i++)
                {
                    @Html.RadioButtonFor(m => m.PaymentOptions.RegistrationPaymentTypes[i], @Model.PendingRegistrations.PaymentOptions.RegistrationPaymentTypes)
                    @Html.LabelFor(m => m.PaymentOptions.RegistrationPaymentTypes[i], @Model.PendingRegistrations.PaymentOptions.RegistrationPaymentTypes)
                    <br />
                }
            </li>
        }
    </ol>
</div

Models

public class ViewModelPendingRegistrations
{
    public ViewModelPendingRegistrations()
    {
        PendingRegistrations = new List<ViewModelPendingRegistration>();
        Errors = new List<string>();
        this.TempCart = new List<SalesTax.TempCart>();
        this.Cart = new List<Registrations.Cart>();
        this.PaymentOptions = new ViewModelPaymentOptions();
    }
    public ViewModelPaymentOptions PaymentOptions { get; set; }
    public List<Domain.ViewModel.Registrations.Cart> Cart { get; set; }
    public List<ViewModelPendingRegistration> PendingRegistrations { get; set; }
    public bool IsBusinessAdmin { get; set; }
    public bool HasBusinessRegistrations { get; set; }
    public bool HasIndividualRegistrations { get; set; }
    public List<string> Errors { get; set; }

here is Model.PendingRegistrations.Cart

public class Cart
{
    public Cart()
    {
        this.AdditionalFees = new List<ViewModelAdditionalFee>();
        this.PaymentOptions = new ViewModelPaymentOptions();
    }

    public List<ViewModelRegistrationRequirement> Requirements { get; set; }
    public ViewModelPaymentOptions PaymentOptions { get; set; }
    public int SelectedPaymentOption { get; set; }
    public string EntityName { get; set; }
}

this is my controller method

public ActionResult PaymentOptions(Domain.ViewModel.PaymentModel model)
{
    var pendingRegistrations = new BusinessLayer.Registration().GetPendingRegistrations(false, false, AppUser.UserId, true);
    var paymentdetails = new BusinessLayer.PaymentType().GetAllActive();
    var regs = pendingRegistrations.Cart;
    model.PendingRegistrations.Cart = regs;
    var payments = new BusinessLayer.RegistrationTypePaymentType().GetAll();
    foreach (var pendingReg in regs)
    {
        model.PaymentOptions.RegistrationPaymentTypes = payments.Where(x => x.RegistrationTypeID == pendingReg.RegistrationTypeID).ToList();
        foreach (var pmt in pendingReg.PaymentOptions.RegistrationPaymentTypes)
        {
            var pmtnames = paymentdetails.Where(x => x.PaymentTypeID == pmt.PaymentTypeID).FirstOrDefault();
            pmt.PaymentName = pmtnames.Name;
        }
    }
    return View(model);
}

and lastly this is what the view looks like with this code and lastly this is what the view looks like with this code.

Chiefster
  • 61
  • 1
  • 10
  • 1
    You cannot bind a radio button to a collection of complex objects which is what `PaymentOptions`. You need a (say) `int SelectedPayment` property to bind the selected value to –  Jun 05 '18 at 23:34
  • would it be a list of int? something like Public List SelectedPayment – Chiefster Jun 05 '18 at 23:50
  • No. Just `public int SelectedPayment { get; set; }` (although its not clear why you have nested loops and what you are really trying to bind to) –  Jun 05 '18 at 23:54
  • your solution worked! but it created another bug. which is probably related to the nested foreach loops. I have more than one Registration, and each registration can have more than one payment option but its creating all the radio buttons under one group, and only allows for one selection... but i guess thats another question ill have to post lol. thanks again for your help! – Chiefster Jun 06 '18 at 00:18
  • Then you models are all wrong for a start, and you cannot use a `foreach` loop to generate form controls for a collection. Start by reading [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) and then you need to create a view model that properly represents what you want in the view –  Jun 06 '18 at 00:20
  • Also [this answer](https://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) might help to understand how your view model and view should be structured –  Jun 06 '18 at 00:22
  • wow ok, thank you! I have some more research to do – Chiefster Jun 06 '18 at 00:32
  • I've made changes to my code but it is still not working as expected, any chance you can look at the updated code? – Chiefster Jun 08 '18 at 05:07
  • There is still a lot wrong with your code. But you need to show your models and their properties (all you have shown is their constructors), and what the `ViewModelPendingRegistrations` model is (in particular what its `Cart` property is) –  Jun 08 '18 at 05:15
  • @StephenMuecke i've posted the updated code. – Chiefster Jun 08 '18 at 05:27
  • Will be a few hours before I get a chance to look at your code –  Jun 08 '18 at 05:33
  • Hey no problem at all! I'm forever greatful for your help thus far! – Chiefster Jun 08 '18 at 05:34
  • You do not appear to have understood the links I gave you previously. No time left today but I will have a look tomorrow. –  Jun 08 '18 at 11:08
  • ok thanks ill go back and study those links again meanwhile – Chiefster Jun 08 '18 at 14:14

0 Answers0