asp.net mvc - MVC Checkbox value not working -
hi have check box checklist programme making, type bool? can pass null value if reply not applicable (they should leave yes , no blank), otherwise should tick yes or no..my problem how can save reply answer property.
view:
yes @html.checkbox("chkyes", model.questionnaires[itemindex].answer.hasvalue ? bool.parse(model.questionnaires[itemindex].answer.tostring()):false) no @html.checkbox("chkno", model.questionnaires[itemindex].answer.hasvalue ? !bool.parse(model.questionnaires[itemindex].answer.tostring()) : false)
model:
public bool? reply { get; set; }
changed view checkbox radiobutton:
yes @html.radiobuttonfor(modelitem => modelitem.questionnaires[itemindex].answer,true, new { id = "rbyes"}) no @html.radiobuttonfor(modelitem => modelitem.questionnaires[itemindex].answer,false, new { id = "rbno"}) not applicable @html.radiobuttonfor(modelitem => modelitem.questionnaires[itemindex].answer,null, new { id = "rbnotapp"})
my problem how pass null value when not applicable?
you 2 checkboxes (and associated hidden inputs) rendered as
<input type="checkbox" name="chkyes" ...> <input type="hidden" name="chkyes" ...> <input type="checkbox" name="chkno" ...> <input type="hidden" name="chkno" ...>
which post properties named chkyes
, chkno
(which don't exist) property name answer
. can utilize @html.editorfor(m => m.questionnaires[itemindex].answer)
render dropdown 3 values (true/false/not set) or utilize 3 radio button indicate state.
note cannot utilize checkbox
nullable bool
. checkbox has 2 states (checked = true or unchecked = false) whereas nullable bool
has 3 states (true, false , null). in add-on checkbox not post value if unchecked
if utilize radio buttons, then
yes @html.radiobuttonfor(modelitem => modelitem.questionnaires[itemindex].answer, true, new { id = "rbyes"}) no @html.radiobuttonfor(modelitem => modelitem.questionnaires[itemindex].answer, false, new { id = "rbno"}) not applicable @html.radiobuttonfor(modelitem => modelitem.questionnaires[itemindex].answer, string.empty, model.questionnaires[itemindex].answer.hasvalue ? (object)new { id = "rbnotapp" } : (object)new { id = "rbnotapp", @checked = "checked" })
asp.net-mvc razor checkbox
No comments:
Post a Comment