I am working on a ASP.NET MVC3 code to bind the CheckBoxes using the data from the column of my SQL Database present in App_Data directory.
My table SysUser3 contains two columns with values as follows:
ProductName || ProductId
Pencil 1
Eraser 2
Pen 3
Model:
public class StoreModel
{
public List<ProductModel> Products { get; set; }
}
public class ProductModel
{
public string ProductName { get; set; }
public bool Selected { get; set; }
}
Controller:
[HttpGet]
public ActionResult CheckView()
{
var model = new StoreModel
{
Products = m.GetCheckBoxes()
};
return View(model);
}
//GetCheckBoxes method()
public IList<ProductModel> GetCheckBoxes()
{
IList<ProductModel> items = new List<ProductModel>();
using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DeveloperReport.mdf;User Instance=true"))
{
con.Open();
string cmdString = "SELECT ProductName FROM SysUser3";
using (SqlCommand cmd = new SqlCommand(cmdString, con))
{
using (SqlDataReader dataRead = cmd.ExecuteReader())
{
while (dataRead.Read())
{
items.Add(new ProductModel
{
Text = dataRead["ProductName"].ToString()
});
}
}
}
}
return items;
}
View:
@using (Html.BeginForm())
{
<div>
@Html.HiddenFor(x => x.ProductName)
@Html.CheckBoxFor(x => x.Selected)
@Html.LabelFor(x => x.Selected, Model.ProductName)
</div>
}
However, my code isn't working fine and I am not able to see the binding taking place. I just get a empty checkbox when I run the code.
Can someone tell me what am I doing wrong
Thanks in advance