MVC generates two input fields for the same bool property. How do I stop it?
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="开发者_JS百科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.
Are you using a custom model binder or metadata provider by any chance?
精彩评论