DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
222 stars 79 forks source link

ButtonGroup not model binding on HttpPost #364

Open tperri opened 9 years ago

tperri commented 9 years ago

I have this:

    <div class="control-group">
        @Html.LabelFor(x => x.ManagementStatusId, new { @class = "control-label" })
        <div class="controls">
            @using (var g = Html.Bootstrap().Begin(new ButtonGroup().Data(new { toggle = "buttons" }).HtmlAttributes(new { id = "grpManagementStatusId" })))
            {
                for (int i = 0; i < Model.Statuses.Count; i++)
                {
                    @g.Button().Text(Model.Statuses[i].Description).Value(Model.Statuses[i].StatusId.ToString()).Class(Model.Statuses[i].BtnClass).Name("ManagementStatusId").Id("ManagementStatusId_" + i.ToString())
                }
            }
        </div>
    </div>

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?

DmitryEfimenko commented 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.

tperri commented 9 years ago

Yes, I am trying to bind the selected button to the model.

The model has a property of int ManagementStatusId

DmitryEfimenko commented 9 years ago

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);
})
DmitryEfimenko commented 9 years ago

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.