模型绑定.NET 4.5 webforms下拉列表错误

时间:2016-08-07 20:02:09

标签: asp.net .net binding model webforms

我有一个使用.net 4.5的小型webforms应用程序,我正在测试模型绑定。我有三种型号,CatergoryItem和Food。

public class  Category
{
    public int Id { get; set; }
    public string TypeName { get; set; }
}

public class CategoryItem
{
    public int Id { get; set; }
    public String Name { get; set; }

}

public class Food
{
    public Category ProductType { get; set; }
    public CategoryItem Product { get; set; }

    public Food()
    {
        ProductType = new Category();
        Product = new CategoryItem();
    }
}

我填写了一个带有食品类别的下拉菜单(" Meat"," Vegetables"),另一个使用所选类别作为填充下拉菜单的类型。 我希望有一个绑定到Food对象的表单,并使用下拉列表中的选项填充此对象。

这是标记

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

    <asp:FormView ID="SampleForm" runat="server" ItemType="WebApplication2.Food" DefaultMode="Insert" InsertMethod="AddFood">
        <InsertItemTemplate>
            <asp:DropDownList
                DataTextField="TypeName"
                AppendDataBoundItems="True"
                DataValueField="Id"
                AutoPostBack="True"
                runat="server"
                ID="Types"
                ItemType="WebApplication2.Category"
                SelectMethod="GetProductTypes"
                SelectedValue="<%# BindItem.ProductType.Id %>">

                <asp:ListItem Text="Select ProductType" Value="0"></asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList
                DataTextField="Name"
                DataValueField="Id"

                AutoPostBack="True"
                runat="server"
                ID="Products"
                ItemType="WebApplication2.CategoryItem"
                SelectMethod="GetProducts"
                SelectedValue="<%# BindItem.Product.Id %>"
                >
            </asp:DropDownList>
            <asp:Button runat="server" CommandName="Insert" Text="Insert"/>
        </InsertItemTemplate>
        <EditItemTemplate></EditItemTemplate>

    </asp:FormView>
</asp:Content>

以下是

背后的代码
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public IEnumerable<Category> GetProductTypes()
    {
        var list = new List<Category>();

        list.Add(new Category() { Id = 2, TypeName = "Meat" });
        list.Add(new Category() { Id = 1, TypeName = "Vegetables" });

        return list;
    }

    public IEnumerable<CategoryItem> GetProducts([Control("Types")] int?                 productTypeId)
    {
        var list = new List<CategoryItem>();

        list.Add(new CategoryItem(){Id = 0, Name = "Select Food"});

        if (!productTypeId.HasValue)
        {
            return list;
        }

        if (productTypeId == 2)
        {
            list.Add(new CategoryItem(){Id = 3,Name = "Beef"});
            list.Add(new CategoryItem(){Id = 4,Name = "Fish"});
        }
        else if (productTypeId == 1)
        {
            list.Add(new CategoryItem() { Id = 5, Name = "Carrot" });
            list.Add(new CategoryItem() { Id = 7, Name = "Potato" });
        }

        return list;
    }

    public void AddFood(Food food)
    {
        TryUpdateModel(food);

    }

}

我设法在类别更改时获取下拉列表以进行填充和刷新。我想使用下拉框中的所选项更新模型。我设法填充了类别,但在尝试绑定categoryitem时出现以下错误:

&#39; /&#39;中的服务器错误应用

数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用。

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用。

来源错误:

Line 18:                 <asp:ListItem Text="Select ProductType" Value="0"></asp:ListItem>
Line 19:             </asp:DropDownList>
Line 20:             <asp:DropDownList
Line 21:                 DataTextField="Name"
Line 22:                 DataValueField="Id"

Source File: c:\Users\Nemo\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\Default.aspx    Line: 20 

Stack Trace: 


[InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]
   System.Web.UI.Page.GetDataItem() +3038873
   ASP.default_aspx.__DataBinding__control9(Object sender, EventArgs e) in c:\Users\Nemo\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\Default.aspx:20
   System.Web.UI.Control.OnDataBinding(EventArgs e) +84
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +20
   System.Web.UI.WebControls.ListControl.PerformSelect() +37
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
   System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +23
   System.Web.UI.Control.PreRenderRecursiveInternal() +88
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4775

如果我删除&#34; SelectedValue&#34;从第二个下拉列表绑定,它可以工作,但我插入时不会将数据绑定到Food对象。

我如何获得我的&#34;食物&#34;对象填充了两个下拉列表中的数据,并让其中一个下拉列表被另一个下载?

0 个答案:

没有答案