Is there a way to bind a complex object to radiobutton

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 01:21:31

问题


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.



来源:https://stackoverflow.com/questions/27233496/is-there-a-way-to-bind-a-complex-object-to-radiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!