tags:

views:

52

answers:

1

C#, MVC 2 RC 2...

I have the following property on my model class:

[Required]
[DataType(DataType.Custom)]
[DisplayName("Show Favourites")]
public bool ShowFavourites { get; set; }

And the following HTML in the view ASPX:

    <div class="editor-label">
        <%= Html.LabelFor(model => model.ShowFavourites) %>
    </div>
    <div class="editor-field">
        <%= Html.CheckBoxFor(model => model.ShowFavourites) %>
        <%= Html.ValidationMessageFor(model => model.ShowFavourites) %>
    </div>

And I do the following in my action:

MyModel model = new MyModel();
if (ModelState.IsValid)
    bool success = TryUpdateModel(model);

While the success value is true, the model object is not updated. I've looked at the rendered HTML in the browser and it shows:

<div class="editor-label"> 
                <label for="ShowFavourites">Show Favourites</label> 
            </div> 
            <div class="editor-field"> 
                <input id="ShowFavourites" name="ShowFavourites" type="checkbox" value="true" />
                <input name="ShowFavourites" type="hidden" value="false" />
</div>

Obviously, the problem is the two input fields (specifically, the hidden field) with the same name attribute, but how do I stop it doing this? Other than the code within the action method, this is all generated code by the IDE. Putting a debug point in the action code shows that the Request.Params["ShowFavourites"] value is "true,false", of course.

Thanks,

Matt.

+2  A: 

Are you using a custom model binder or metadata provider by any chance?

amarsuperstar
Yes! I am! Thank you! How did you know?!
Matt W