asp:表单中的CheckBoxList
我正在尝试将复选框列表放入 asp.NET MVC 2 中的一个简单表单中。这是它的代码:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("HandSurvey", "Resources", FormMethod.Post, new { runat="server" }))
{ %>
<asp:CheckBoxList id="chkListChemical" runat="server">
<asp:ListItem>Fiberglass & resins</asp:ListItem>
<asp:ListItem>Heavy duty oil paints & stains</asp:ListItem>
<asp:ListItem>Mechanics - tools, grease/oil</asp:ListItem>
<asp:ListItem>Metalworking fluids</asp:ListItem>
<asp:ListItem>Paint & Stains in use</asp:ListItem>
<asp:ListItem>Exposure to solvents</asp:ListItem>
<asp:ListItem>Difficult soils</asp:ListItem>
<asp:ListItem>Hydrocarbons</asp:ListItem>
</asp:CheckBoxList>
<% }
%>
</asp:Content>
当我点击此页面时,它给出了此错误:
“CheckBox”类型的控件“ctl00_MainContent_chkListChemical_0”必须放置在带有 runat=server 的表单标记内。
我认为我正确指定了 runat 属性。或者我在这里还缺少其他东西吗?如果我不使用辅助类而只使用常规表单标签,它就可以工作。
I am trying to put a checkboxlist onto a simple form in asp.NET MVC 2. This is the code for it:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("HandSurvey", "Resources", FormMethod.Post, new { runat="server" }))
{ %>
<asp:CheckBoxList id="chkListChemical" runat="server">
<asp:ListItem>Fiberglass & resins</asp:ListItem>
<asp:ListItem>Heavy duty oil paints & stains</asp:ListItem>
<asp:ListItem>Mechanics - tools, grease/oil</asp:ListItem>
<asp:ListItem>Metalworking fluids</asp:ListItem>
<asp:ListItem>Paint & Stains in use</asp:ListItem>
<asp:ListItem>Exposure to solvents</asp:ListItem>
<asp:ListItem>Difficult soils</asp:ListItem>
<asp:ListItem>Hydrocarbons</asp:ListItem>
</asp:CheckBoxList>
<% }
%>
</asp:Content>
When I hit this page it gives this error:
Control 'ctl00_MainContent_chkListChemical_0' of type 'CheckBox' must be placed inside a form tag with runat=server.
I thought I was specifying the runat attribute correctly. Or is there something else that I am missing here? If I don't use the helper class and just use a regular form tag, it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ASP.NET MVC 中,您应该避免使用服务器控件。基本上,不应使用具有
runat="server"
的所有内容(WebForms 视图引擎中的内容占位符除外)。在 ASP.NET MVC 中,您设计视图模型:然后您有操纵视图模型的控制器操作:
最后您有一个强类型视图:
更新:
按照注释部分的要求,以便从数据库动态生成这些复选框调整视图模型是一个简单的问题:
现在我们将有控制器操作来返回此视图模型的列表:
视图现在将简单地变为:
最后一部分是为我们的视图模型定义编辑器模板(
~/Views/Shared/EditorTemplates/ItemViewModel.ascx
):In ASP.NET MVC you should avoid using server controls. Basically everything that has the
runat="server"
should not be used (except the content placeholder in WebForms view engine). In ASP.NET MVC you design view models:then you have controller actions that manipulate the view model:
and finally you have a strongly typed view:
UPDATE:
As requested in the comments section in order to have those checkboxes dynamically generated from a database it is a simple matter of adapting our view model:
and now we will have our controller action to return a list of this view model:
and the view will now simply become:
and the last part would be to define an editor template for our view model (
~/Views/Shared/EditorTemplates/ItemViewModel.ascx
):