开发者

ASP MVC Checkbox checked state bound to value on render

开发者 https://www.devze.com 2023-01-11 16:34 出处:网络
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 po

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) %>
  1. It's strongly typed an handles the naming/etc. itself.
  2. Using the <%: syntax automatically encodes html characters in your own strings but doesn't affect HTMLStrings returned from various .Net functions (like CheckboxFor()).
0

精彩评论

暂无评论...
验证码 换一张
取 消