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:

  1. how display existing selected option in edit view in dropdown
  2. 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

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -