0
I have a model with several booleans, I need to take their values through a Formcollection and save in the SQL database (does not accept Boolean, so the field is as 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 mounting control for 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;
}
Dude, you should seriously think about giving up the use of
FormCollection
in order to use a typed object.– Leonel Sanches da Silva