0

My Cart viewmodel is

public class CartViewModel
{
    public List<PaymentMethods> PaymentMethod { get; set; }
}

My PaymentMethods class

public partial class PaymentMethods
{
    public int PaymentMethodID { get; set; }
    public string PaymentName { get; set; }
    public bool Active { get; set; }
}

My view is

@foreach (PaymentMethods pm in Model.PaymentMethod)
  {
      if (pm.Active)
      {
          <li>
              @Html.RadioButtonFor(c => c.PaymentMethod, pm.PaymentMethodID, new { id = "pay" + pm.PaymentMethodID, @class = "paymethod" })
              <label for="@("pay" + pm.PaymentMethodID)" class="checkbox">@pm.PaymentName</label>
          </li>
      }
   }

And the controller accepts the CartViewModel as parameter.

[HttpPost]
public ActionResult Index(CartViewModel CartViewModel)
{

}

The question is why when i choose a Payment method and submit the form, the controller has PaymentMethod.Count equal to zero?

The selection is not passed on the controller. What am i doing wrong here?

tereško
  • 58,060
  • 25
  • 98
  • 150
OrElse
  • 9,709
  • 39
  • 140
  • 253
  • the lamda should point to the selected variable and not to the list. see here http://stackoverflow.com/questions/22174475/multiple-radio-button-groups-in-mvc-4-razor/22174654#22174654 – Matt Bodily Nov 23 '15 at 17:27
  • Where are you adding a Payment method to the list? – BillRuhl Nov 23 '15 at 17:46

2 Answers2

2

Your attempting to bind a radio button group to a collection of complex objects. A radio button only posts the value of the selected button (in your case the value of the selected PaymentMethodID property). You need a separate property in your model to bind to.

public class CartViewModel
{
    public int SelectedPaymentMethod { get; set; }
    public IEnumerable<PaymentMethods> PaymentMethods { get; set; }
}

and modify your view to

@foreach (PaymentMethods pm in Model.PaymentMethod)
{
    <li>
        <label>
            @Html.RadioButtonFor(c => c.SelectedPaymentMethod, pm.PaymentMethodID, new { id = "", @class = "paymethod" })
            <span>@pm.PaymentName</span>
        </label>
    </li>
}

Now when you submit the form, the value of SelectedPaymentMethod will be the value of the selected PaymentMethodID property.

Side note: Rather than including the if (pm.Active) statement in the view, consider filtering the collection in the controller (e.g. model.PaymentMethods = db.PaymentMethods.Where(p => p.IsActive);)

0

Problem with name generation of Radio buttons and due to which ModelBinding does not happen at server. Also you should have an item binding for selected item.

Add the model property as

public int SelectedPayMethodID { get; set;}

Change the implementation as follows

@for (int index =0; index < Model.PaymentMethod; index++)
  {
      if (Model.PaymentMethod[index].Active)
      {
          <li>
              @Html.RadioButtonFor(model => model.SelectedPayMethodID, Model.PaymentMethod[index].PaymentMethodID, new { id = "pay" + Model.PaymentMethod[index].PaymentMethodID, @class = "paymethod" })
              <label for="@("pay" + Model.PaymentMethod[index].PaymentMethodID)" class="checkbox">@pm.PaymentName</label>
          </li>
      }
}
user1672994
  • 10,509
  • 1
  • 19
  • 32