ASP MVC Checkbox checked state bound to value on render
I'm new to MVC. I've got this all working nicely except when the page loads the checkbox is not set to the value of the object from the model. I am certainly missing something simple - hope you can point it out to me.
I'm using a ViewModel class for the view, so my view page has this code:
<%: Html.CheckBox("IsX", Model.Contact.IsX) %>
I've been looking through the available overloads but just can't figure out what I need 开发者_如何学Pythonto put in there...
Any help greatly appreciated.
Tim.
Silly question, are you returning the viewmodel from the action?
eg
Function SomeAction(Moel as ViewModelType) As ActionResult
If Model.IsValid()
'' Do Stuff
Else
Return view(Model)
End If
End Function
Also, in my code, I use the following syntax:
<%: Html.CheckBoxFor((x) => x.CheckboxPropertyOnModelName) %>
- It's strongly typed an handles the naming/etc. itself.
- Using the <%: syntax automatically encodes html characters in your own strings but doesn't affect HTMLStrings returned from various .Net functions (like CheckboxFor()).
精彩评论