c# - How to save radiobutton data in MVC controller edit method? -
in edit page, have 2 radio buttons gender, once user selected gender male or female, value should saved in database.
i tried solutions proposed, none works.
here view page:
<div class="form-group"> @html.labelfor(model => model.ismale, htmlattributes: new { @class = "control-label col-md-2" }) <div class="radioleft col-sm-3"> @html.radiobuttonfor(model => model.ismale, "male", new { @checked = true })male @html.radiobuttonfor(model => model.ismale, "female", new { @checked = false })female @html.validationmessagefor(model => model.ismale, "", new { @class = "text-danger" }) </div> </div>
here controller edit method:
[httppost] [validateantiforgerytoken] public actionresult edit( webuser webuser) { if (modelstate.isvalid) { db.entry(webuser).state = entitystate.modified; db.savechanges(); return redirecttoaction("index"); } return view(webuser); }
your type nullable<bool>
, you're sending either string "male"
or "female"
. how modelbinder supposed know 1 means true , means false? if you've got bool
type need pass boolean, example:
@html.radiobuttonfor(model => model.ismale, true) male @html.radiobuttonfor(model => model.ismale, false) female
Comments
Post a Comment