asp.net mvc 3 - How to display bool values in a view and save the value to DB -
i have table has bit values (true/false)
table definition:
characterid int isactive bit userid uniqueidentifier
i have 2 problems:
- how display existing selected option in edit view in dropdown
- i need save value (yes/no) true , false in database.
here have attempted far:
<div class="editor-label"> @html.labelfor(model => model.isactive) </div> <div class="editor-field"> @html.dropdownlist("", new selectlistitem[] { new selectlistitem() { text = "yes", value = "true", selected = model.isactive }, new selectlistitem() { text = "no", value = "false", selected = !model.isactive }}) </div>
assuming model.isactive declared bool
:
wouldn't using checkbox bit more intuitive user , require less clicks? in case use:
@html.editorfor(model => model.isactive)
if want dropdowns, might provide working implementation: https://stackoverflow.com/a/4036922/1373170
applied context, believe be:
@html.dropdownlistfor(model => model.isactive, new selectlist(new selectlistitem[] { new selectlistitem() { text = "yes", value = "true" }, new selectlistitem() { text = "no", value = "false"}}, model.isactive.tostring())
now, saving database i'd have know if using ef, l2s, etc. imagine have action in controller set saving. in case receiving instance of model parameter. using dropdownlistfor instead of dropdownlist, model should bound automatically mvc's default modelbinder
, , should able map database entity , store it.
Comments
Post a Comment