I'm making an web app for my intership and here is my question. I'v got a form that users have to complete (only checkboxes to check or not) I already do the code to store the data in my database (as true/false) when they submit. But I also want to to display the checkboxe check or not when they return on that page depending on how they have been stored in database. And I don't find/know how to do it.
Here is the code i'v make for save them in databse if it could help ^^ Thx !
CONTROLLER
if (Request.Form["submit"] != null)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-GoodFood-20171116084216.mdf;Initial Catalog=aspnet-GoodFood-20171116084216;Integrated Security=True";
using (conn)
{
String query = "UPDATE PreferencesModels SET Lait=@Lait, oeufs=@oeufs, Arachides=@Arachides, coque=@coque, sesame=@sesame, " +
"gluten=@gluten, soja=@soja, poisson=@poisson, crustace=@crustace, mollusques=@mollusques, moutarde=@moutarde," +
"diabete=@diabete, cholesterol=@cholesterol, palme=@palme" +
" WHERE email=@email";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Lait", model.Lait);
cmd.Parameters.AddWithValue("@oeufs", model.Oeufs);
cmd.Parameters.AddWithValue("@arachides", model.Arachides);
cmd.Parameters.AddWithValue("@coque", model.Coque);
cmd.Parameters.AddWithValue("@sesame", model.Sesame);
cmd.Parameters.AddWithValue("@gluten", model.Gluten);
cmd.Parameters.AddWithValue("@soja", model.Soja);
cmd.Parameters.AddWithValue("@poisson", model.Poisson);
cmd.Parameters.AddWithValue("@crustace", model.Crustace);
cmd.Parameters.AddWithValue("@mollusques", model.Mollusques);
cmd.Parameters.AddWithValue("@moutarde", model.Moutarde);
cmd.Parameters.AddWithValue("@diabete", model.Diabete);
cmd.Parameters.AddWithValue("@cholesterol", model.Cholesterol);
cmd.Parameters.AddWithValue("@palme", model.Palme);
cmd.Parameters.AddWithValue("@email", Session["email"]);
conn.Open();
cmd.ExecuteNonQuery();
return View("Scanner");
}
}
}
VIEW
<fieldset>
<div class="test1">
@Html.CheckBoxFor(model => model.Lait)
@Html.LabelFor(model => model.Lait)
</div>
</fieldset>
<fieldset>
<div class="test1">
@Html.CheckBoxFor(model => model.Oeufs)
@Html.LabelFor(model => model.Oeufs)
</div>
</fieldset>
<fieldset>
<div class="test1">
@Html.CheckBoxFor(model => model.Arachides)
@Html.LabelFor(model => model.Arachides)
</div>
</fieldset>
MODEL :
[Display(Name = "Lait")]
public bool Lait { get; set; }
[Display(Name = "Oeufs")]
public bool Oeufs { get; set; }
[Display(Name = "Arachides")]
Aucun commentaire:
Enregistrer un commentaire