Asp.net 模型绑定.NET 4.5 webforms dropdownlist错误
我有一个使用.NET4.5的小型webforms应用程序,我正在测试模型绑定。我有三种型号,一种是商品,一种是食品Asp.net 模型绑定.NET 4.5 webforms dropdownlist错误,asp.net,.net,binding,model,webforms,Asp.net,.net,Binding,Model,Webforms,我有一个使用.NET4.5的小型webforms应用程序,我正在测试模型绑定。我有三种型号,一种是商品,一种是食品 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; s
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();
}
}
我用食物类别(“肉”、“蔬菜”)填充其中一个下拉列表,另一个使用所选类别作为填充下拉列表的类型。
我想要有一个绑定到食物对象的表单,并用下拉列表中的选择填充这个对象
这是加价单
<%@ 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);
}
}
public分部类\u默认值:第页
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共IEnumerable GetProductTypes()
{
var list=新列表();
添加(新类别(){Id=2,TypeName=“Meat”});
添加(新类别(){Id=1,TypeName=“蔬菜”});
退货清单;
}
公共IEnumerable GetProducts([Control(“Types”)]int?productTypeId)
{
var list=新列表();
添加(新的CategoryItem(){Id=0,Name=“Select Food”});
如果(!productTypeId.HasValue)
{
退货清单;
}
if(productTypeId==2)
{
添加(新的CategoryItem(){Id=3,Name=“Beef”});
添加(新的CategoryItem(){Id=4,Name=“Fish”});
}
else if(productTypeId==1)
{
添加(新的CategoryItem(){Id=5,Name=“Carrot”});
添加(新的CategoryItem(){Id=7,Name=“Potato”});
}
退货清单;
}
公共食品(食品)
{
TryUpdateModel(食品);
}
}
我设法让下拉列表在类别更改时填充和刷新。我想用下拉框中的选定项更新模型。我成功地填充了类别,但在尝试绑定categoryitem时,出现以下错误:
“/”应用程序中出现服务器错误
诸如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
第18行:
第19行:
第20行: