mardi 29 octobre 2019

How to display and save checkbox value from bit field in MVC?

I have a bit value in my database for something called AMD. I'm trying to display it on my page using razor and MVC, but it doesn't appear or save properly. My controller displays and saves the entire model.

My test case has a value of 0, which I try to convert to boolean, but get an error. I'm not sure if the best way to do it is to convert it to boolean, or if I could just outright use 1 and 0 to check the checkbox. I'll also need it to be 1 or 0 when it saves.

Things I've tried in the view:

(cannot convert int? to bool)

@Html.CheckBox(Convert.ToBoolean(Model.Detail.AMD))

(cannot convert int? to bool)

<label class="label4">@Html.CheckBoxFor(x => x.Detail.AMD)</label>

(always checked, despite the AMD value in the database being 0; value also stays 0 when clicking save)

<input type="checkbox" name="isChecked" class="checkbox" value="@Model.Detail.AMD"
checked="@Html.Raw(Convert.ToBoolean(Model.Detail.AMD) ? "checked" : string.Empty)" />

The controller is very generic:

(page load)

Manager.GetDetails(id);

(page save)

Manager.SaveDetails(model.Detail);

Any help is appreciated. I am open to learning.




Aucun commentaire:

Enregistrer un commentaire