I have a model with several booleans, I need to get their values through a FormCollection and save them to the SQL database (it does not accept boolean, so the field is a bit).
My View:
<div class="editor-field">
@Html.RadioButtonFor(model => model.Sex,"false", false)F
@Html.RadioButtonFor(model => model.Sex,"true", true)M
</div>
Action:
public ActionResult Create(FormCollection form)
{
var pSimpleUser = new SimpleUserr()
{
IdSimpleUser = 0,
Sex = Convert.ToBoolean (form["sex"]),
};
if (ModelState.IsValid)
{
using (SimpleUserDAO dao = new SimpleUserDAO())
if (dao.SaveSimpleUser(pSimpleUser))
{
ViewBag.AlertMessage = "Salvo Com Sucesso";
return View();
}
}
ViewBag.AlertMessage = "Ocorreu um problema ao salver";
return View(pSimpleUser);
}
}
}
Function with the command assembly for the Procedure:
public bool SaveSimpleUser(SimpleUserr pSimpleUser)
{
if (pSimpleUser !=null)
{
using (_context)
{
SqlCommand cmd = new SqlCommand("SaveSimpleUser");
cmd.Parameters.AddWithValue("@idSimpleUser", pSimpleUser.IdSimpleUser);
cmd.Parameters.AddWithValue("@sex", pSimpleUser.Sex);
pSimpleUser.IsPaceMaker);
cmd.CommandType = CommandType.StoredProcedure;
this._context.ExecuteProcedure(cmd);
return true;
}
}
return false;
}