Open tperri opened 9 years ago
The question is not clear to me. Are you trying to bind Buttons to the Model? Please review what you wrote and be more specific.
Yes, I am trying to bind the selected button to the model.
The model has a property of int ManagementStatusId
buttons are not inputs. They don't get bound to Model automatically. You'd have to have a custom solution for this. You could add a hidden input and write some script to update value of hidden input on button click.
@Html.HiddenFor(x => x.ManagementStatusId)
for (int i = 0; i < Model.Statuses.Count; i++)
{
@g.Button().Text(Model.Statuses[i].Description).Class("my-btn" + Model.Statuses[i].BtnClass).Data(new { id = Model.Statuses[i].StatusId.ToString() })
}
$(document).on('click', '.my-btn', function(){
var self = this;
var id = self.attr('data-id');
$('#ManagementStatusId').val(id);
})
I'm sorry I couldn't get back to this issue earlier. Now that I looked at it again, the solution may be easier that I I originally thought.
If I understand situation correctly, you have a list of items (Model.Statuses
), and you want to create a radio button group for each item in this list. There is already a method in BMVC that's designed to address exactly this situation. For your case you'd write something like:
@(
Html.Bootstrap()
.RadioButtonListFor(
model => model.ManagementStatusId,
model => model.Statuses,
status => status.StatusId,
status => status.Description)
.InputHtmlAttributes(x => { @class = "my-btn" + x.BtnClass, data_id = x.StatusId })
.AsButtonGroup()
)
Please let me know if this is what you wanted to achieve.
I have this:
ManagementStatusId is a property on the root of my ViewModel, and is always 0. Statuses is a List where there is an Id, Description, and CssClass.
I don't it matters but I am loading the partial view this is in into a modal and am getting the data input into the other entry fields (textboxes) when the form posts.
Any ideas?