asp:表单中的CheckBoxList

发布于 2024-11-28 10:16:53 字数 1293 浏览 0 评论 0原文

我正在尝试将复选框列表放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-12-05 10:16:53

在 ASP.NET MVC 中,您应该避免使用服务器控件。基本上,不应使用具有 runat="server" 的所有内容(WebForms 视图引擎中的内容占位符除外)。在 ASP.NET MVC 中,您设计视图模型:

public class MyViewModel
{
    [DisplayName("Fiberglass & resins")]
    public bool Item1 { get; set; }

    [DisplayName("Heavy duty oil paints & stains")]
    public bool Item2 { get; set; }

    ...
}

然后您有操纵视图模型的控制器操作:

// Action that renders the view
public ActionResult Index()
{
    var model = new MyViewModel();
    return View(model);
}

// Handles the form submission
[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // TODO: Process the results
    return View(model);
}

最后您有一个强类型视图:

<% using (Html.BeginForm("HandSurvey", "Resources")) { %>
    <div>
         <%= Html.CheckBoxFor(x => x.Item1) %> 
         <%= Html.LabelFor(x => x.Item1) %>
    </div>
    <div>
         <%= Html.CheckBoxFor(x => x.Item2) %> 
         <%= Html.LabelFor(x => x.Item2) %>
    </div>

    ...

    <p><input type="submit" value="OK" /></p>
<% } %>

更新:

按照注释部分的要求,以便从数据库动态生成这些复选框调整视图模型是一个简单的问题:

public class ItemViewModel
{
    public int Id { get; set; }
    public string Text { get; set; }
    public bool IsChecked { get; set; }
}

现在我们将有控制器操作来返回此视图模型的列表:

public ActionResult Index()
{
    // TODO: obviously those will come from a database
    var model = new[]
    {
        new ItemViewModel { Id = 1, Text = "Fiberglass & resins" },
        new ItemViewModel { Id = 2, Text = "Heavy duty oil paints & stains" },
    };
    return View(model);
}

视图现在将简单地变为:

<% using (Html.BeginForm("HandSurvey", "Resources")) { %>
    <%= Html.EditorForModel() %>
    <p><input type="submit" value="OK" /></p>
<% } %>

最后一部分是为我们的视图模型定义编辑器模板(~/Views/Shared/EditorTemplates/ItemViewModel.ascx):

<%@ Control 
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.ItemViewModel>" 
%>
<div>
    <%= Html.HiddenFor(x => x.Id) %>
    <%= Html.CheckBoxFor(x => x.IsChecked) %>
    <%= Html.DisplayFor(x => x.Text) %>
</div>

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:

public class MyViewModel
{
    [DisplayName("Fiberglass & resins")]
    public bool Item1 { get; set; }

    [DisplayName("Heavy duty oil paints & stains")]
    public bool Item2 { get; set; }

    ...
}

then you have controller actions that manipulate the view model:

// Action that renders the view
public ActionResult Index()
{
    var model = new MyViewModel();
    return View(model);
}

// Handles the form submission
[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // TODO: Process the results
    return View(model);
}

and finally you have a strongly typed view:

<% using (Html.BeginForm("HandSurvey", "Resources")) { %>
    <div>
         <%= Html.CheckBoxFor(x => x.Item1) %> 
         <%= Html.LabelFor(x => x.Item1) %>
    </div>
    <div>
         <%= Html.CheckBoxFor(x => x.Item2) %> 
         <%= Html.LabelFor(x => x.Item2) %>
    </div>

    ...

    <p><input type="submit" value="OK" /></p>
<% } %>

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:

public class ItemViewModel
{
    public int Id { get; set; }
    public string Text { get; set; }
    public bool IsChecked { get; set; }
}

and now we will have our controller action to return a list of this view model:

public ActionResult Index()
{
    // TODO: obviously those will come from a database
    var model = new[]
    {
        new ItemViewModel { Id = 1, Text = "Fiberglass & resins" },
        new ItemViewModel { Id = 2, Text = "Heavy duty oil paints & stains" },
    };
    return View(model);
}

and the view will now simply become:

<% using (Html.BeginForm("HandSurvey", "Resources")) { %>
    <%= Html.EditorForModel() %>
    <p><input type="submit" value="OK" /></p>
<% } %>

and the last part would be to define an editor template for our view model (~/Views/Shared/EditorTemplates/ItemViewModel.ascx):

<%@ Control 
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.ItemViewModel>" 
%>
<div>
    <%= Html.HiddenFor(x => x.Id) %>
    <%= Html.CheckBoxFor(x => x.IsChecked) %>
    <%= Html.DisplayFor(x => x.Text) %>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文