【问题标题】:asp:CheckBoxList in a formasp:CheckBoxList 的形式
【发布时间】:2011-08-09 14:07:41
【问题描述】:

我正在尝试将复选框列表放在 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 属性。还是我在这里缺少其他东西?如果我不使用辅助类而只使用常规表单标签,它就可以工作。

【问题讨论】:

    标签: asp.net-mvc-2 html-helper


    【解决方案1】:

    在 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>
    <% } %>
    

    更新:

    根据 cmets 部分的要求,为了从数据库中动态生成这些复选框,调整我们的视图模型很简单:

    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>
    

    【讨论】:

    • 伙计,似乎 MVC 让一切变得更加困难。我想使用复选框列表,因为最终列表项将从数据库中读取。因此,将它们中的每一个硬编码为模型中的布尔值对我来说不起作用。如何动态获取复选框?
    • @Nick,MVC 并没有让一切变得更加困难。它与经典的 WebForms 不同 模式。我将更新我的答案以说明如何实现所需的动态行为。
    • 非常感谢您的帮助。这对我来说似乎很有意义,我会尝试一下。
    猜你喜欢
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多