Is there a way to bind a complex object to radiobu

2019-09-07 18:07发布

问题:

I have a list of radiobuttons that I am generating from my model like this:

@foreach (var offer in Model.AvailableOffers)
{                    
  <div>
    @Html.RadioButtonFor(m => m.selectedAvailableOffer, offer)
    @Html.Label(offer.begin_date.Month + "/" + offer.begin_date.Day + " - " + offer.end_date.Month + "/" + offer.end_date.Day + ", " + offer.offer_name)
  </div>
}

Model includes these:

public List<AvailableOffer> AvailableOffers { get; set; }
public AvailableOffer selectedAvailableOffer { get; set; }

See how I am trying to post my offer object back to my controller in the RadioButtonFor method? This is obviously not working, but it's what I want to accomplish. Is there any way to post the whole selected offer object back to my controller or am I stuck adding some sort of ID to my object and only posting back the ID as is suggested here:

Correct way to bind an mvc3 radiobutton to a model

回答1:

Shortly: No, you can't pass whole 'offer' object back to the server. It's better to use only id's on the client side. If you realy want to post back whole offer you can use some kind of serialization to store data from each offer object as a value of radiobutton.