Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
am trying to create filters like in eCommerce website like Amazon, Flipkart.

I have created primary tables for all filters as below.

SQL
create table Work
(
id int identity(1,1) Primary Key Not Null,
work_name varchar(30) Not Null,
isSelected bit Default(0),
created_date Datetime default(GETDATE())
)


Now I want to create a CheckBox List so that a user can filter the products according to the selected details.

What I have tried:

I have created an Editor Template

C#
@model API.Models.Work

@Html.HiddenFor(x=>x.id)
@Html.HiddenFor(x => x.work_name)
@Html.CheckBoxFor(x=>(bool)x.isSelected)    
@Html.DisplayFor(x=>x.work_name)


I am using the template as

C#
@model IEnumerable<API.Models.Work>

@Html.EditorForModel()


I get the below error

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Posted
Updated 18-May-17 23:07pm
Comments
F-ES Sitecore 19-May-17 5:45am    
You might need to create a new model class for the data that allows IsSelected to be a bool rather than a bit then copy the data from your table into that class and use that class as the view model.

1 solution

In your template try
C#
@{
var convertedIsSelector = (bool)Model.isSelected
}
@Html.CheckBoxFor(x => convertedIsSelector)

instead of
C#
@Html.CheckBoxFor(x=>(bool)x.isSelected) 

Not sure whether you need to cast to bool explictly at all.
Also friendly advice. Please note that some of your properties use underscores and some camel case. So unaesthetic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900