I am developing a project using ASP MVC and stored procedures (SQL Server), I want to store checked checkbox items in database. I've tried to add a List<string> type in model in order to access these value and then store them in the database.
The problem that the relational databases are designed specifically to store one value per row/column combination. In order to store more than one value, I must serialize my list into a single value for storage, then deserialize it upon retrieval.
That's my view markup:
<h6>Items</h6>
<ul>
<li>
<label class="anim">
<input type="checkbox" class="checkbox" value="Soups" name="Items">
<span>Soups</span>
</label>
</li>
<li>
<label class="anim">
<input type="checkbox" class="checkbox" value="Burger" name="Items" >
<span>Burger</span>
</label>
</li>
<li>
<label class="anim">
<input type="checkbox" class="checkbox" value="Drinks" name="Items">
<span>Drinks</span>
</label>
</li>
<li>
<label class="anim">
<input type="checkbox" class="checkbox" value="Desserts" name="Items">
<span>Desserts</span>
</label>
</li>
</ul>
Method AddBestellung:
try
{
using (SqlConnection con = new SqlConnection(Connection()))
{
using (SqlCommand cmd = new SqlCommand("AddNewBestellung", con))
{
foreach(var item in bs.Items)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Items", item);
}
// Another saved parameters...
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i >= 1)
return true;
else
return false;
}
}
}
catch(Exception ex)
{
string e = ex.Message;
return false;
}
private static string Connection()
{
return ConfigurationManager.ConnectionStrings["deliverycon"].ToString();
}
Model:
public List<string> Items { get; set; }