使用editortemplate创建复选框列表? [英] Create checkbox list using editortemplate?

查看:80
本文介绍了使用editortemplate创建复选框列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像亚马逊,Flipkart这样的电子商务网站创建过滤器。



我已经为所有过滤器创建了主表,如下所示。



 创建 工作

id int identity 1 1 主要 密钥
work_name varchar 30
isSelected 位<​​/ span> 默认 0 ),
created_date 日期时间 de错误(GETDATE())





现在我想创建一个CheckBox List以便用户可根据所选细节过滤产品。



我尝试过:



我创建了一个编辑模板



 @ 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)





我正在使用模板



 @ model IEnumerable< API.Models.Work> 

@ Html.EditorForModel()





我得到以下错误



System.Web.Mvc.dll中出现System.InvalidOperationException类型的异常,但未在用户代码中处理



附加信息:模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。

解决方案

< blockquote>在你的模板中试试

 @ {
var convertedIsSelector =( bool )Model.isSelected
}
@ Html.CheckBoxFor(x = > convertedIsSelector)



而不是

 @ Html.CheckBoxFor(x = > ;  bool )x.isSelected)



不确定是否需要强制转换为bool明确地说。

也友好的建议。请注意,您的部分房产使用下划线和一些驼色外壳。如此不美观。


am trying to create filters like in eCommerce website like Amazon, Flipkart.

I have created primary tables for all filters as below.

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

@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

@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.

解决方案

In your template try

@{
var convertedIsSelector = (bool)Model.isSelected
}
@Html.CheckBoxFor(x => convertedIsSelector)


instead of

@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.


这篇关于使用editortemplate创建复选框列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆