i have a ProductController which is consists of Create method.
My Model :
public class ProductEntry
{
public Crescent.LinqModel.Product Products
I use this extension:
public static MvcHtmlString CheckBoxListFor(
this HtmlHelper htmlHelper,
Expression>> expression,
IEnumerable multiSelectList,
Object htmlAttributes = null)
{
// Derive property name for checkbox name
var body = expression.Body as MemberExpression;
if (body == null)
return null;
String propertyName = body.Member.Name;
// Get currently select values from the ViewData model
IEnumerable list = expression.Compile().Invoke(htmlHelper.ViewData.Model);
// Convert selected value list to a List for easy manipulation
var selectedValues = new List();
if (list != null)
selectedValues = new List(list).ConvertAll(i => i.ToString());
// Create div
var ulTag = new TagBuilder("ul");
ulTag.AddCssClass("checkBoxList");
ulTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
// Add checkboxes
foreach (var item in multiSelectList)
{
ulTag.InnerHtml += String.Format(
"",
propertyName,
item.Value,
selectedValues.Contains(item.Value) ? "checked=\"checked\"" : "",
item.Text);
}
return MvcHtmlString.Create(ulTag.ToString());
}