加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET Web Forms 4.5模型绑定,其中模型包含一个集合

发布时间:2020-12-15 23:14:52 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试更新旧的Web窗体应用程序以使用4.5中添加的新模型绑定功能,类似于MVC绑定功能. 我无法制作可编辑的FormView,它提供了包含简单成员的单个模型以及其他模型的集合的成员.我需要用户才能编辑父对象的简单属性和子集合的属性. 问题是,当代码尝试更新模
我正在尝试更新旧的Web窗体应用程序以使用4.5中添加的新模型绑定功能,类似于MVC绑定功能.

我无法制作可编辑的FormView,它提供了包含简单成员的单个模型以及其他模型的集合的成员.我需要用户才能编辑父对象的简单属性和子集合的属性.

问题是,当代码尝试更新模型时,模型绑定后,子集合(ProductChoice.Extras)始终为空.

以下是我的型号:

[Serializable]
public class ProductChoice
{
    public ProductChoice()
    {
        Extras = new List<ProductChoiceExtra>();
    }

    public int Quantity { get; set; }
    public int ProductId { get; set; }
    public List<ProductChoiceExtra> Extras { get; set; }
}

[Serializable]
public class ProductChoiceExtra
{
    public int ExtraProductId { get; set; }
    public string ExtraName { get; set; }
    public int ExtraQuantity { get; set; }
}

而我的用户控制码在后面:

public partial class ProductDetails : System.Web.UI.UserControl
{
    private Models.ProductChoice _productChoice;

    protected void Page_Load(object sender,EventArgs e)
    {
        _productChoice = new Models.ProductChoice()
        {
            Quantity = 1,ProductId = 1
        };
        _productChoice.Extras.Add(new Models.ProductChoiceExtra()
        {
            ExtraProductId = 101,ExtraName = "coke",ExtraQuantity = 1
        });
        _productChoice.Extras.Add(new Models.ProductChoiceExtra()
        {
            ExtraProductId = 104,ExtraName = "sprite",ExtraQuantity = 2
        });

    }

    public Models.ProductChoice GetProduct()
    {
        return _productChoice;
    }

    public void UpdateProduct(Models.ProductChoice model)
    {
        /* model.Extras is always null here,it should contain two ProductChoiceExtra objects */

        if (TryUpdateModel(_productChoice) == true)
        {
        }
    }
}

我的控制标记:

<div id="selectOptions">
    <asp:FormView runat="server" ID="fvProductSelection" DefaultMode="Edit"
        ItemType="Models.ProductChoice"
        SelectMethod="GetProduct"
        UpdateMethod="UpdateProduct" >

        <EditItemTemplate>
            <asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/>
            <asp:HiddenField runat="server" ID="ProductId" Value="<%# BindItem.ProductId %>" />
            <asp:TextBox Text ="<%# BindItem.Quantity %>" ID="Quantity" runat="server" />

            <asp:Repeater ID="Extras" ItemType="Models.ProductChoiceExtra" DataSource="<%# BindItem.Extras %>" runat="server">
                <ItemTemplate>
                    <asp:HiddenField Value="<%# BindItem.ExtraProductId %>" ID="ExtraProductId" runat="server"  />
                    <asp:Label Text="<%# BindItem.ExtraName %>" ID="Name" runat="server" />
                    <asp:TextBox Text="<%# BindItem.ExtraQuantity %>" ID="Quantity"  runat="server" />
                </ItemTemplate>
            </asp:Repeater>
        </EditItemTemplate>
    </asp:FormView>
</div>

我已经尝试使Extras属性为BindingList而不是列表,但没有任何区别,Extras集合在UpdateProduct方法中未绑定.

解决方法

不幸的是,我不知道Web窗体是如何完成的,所以我不确定如何用中继器重现这个内容,但是在MVC中,模型绑定器需要索引来重建列表.如果我不得不猜测这是如何在网络表单中完成的,这将是类似的:
<div id="selectOptions">
    <asp:FormView runat="server" ID="fvProductSelection" DefaultMode="Edit"
        ItemType="Models.ProductChoice"
        SelectMethod="GetProduct"
        UpdateMethod="UpdateProduct" >
        <EditItemTemplate>
            <asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/>
            <asp:HiddenField runat="server" ID="ProductId" Value="<%# BindItem.ProductId %>" />
            <asp:TextBox Text ="<%# BindItem.Quantity %>" ID="Quantity" runat="server" />

            <% for (int i = 0; i < BindItem.Extras.Count; i++)
            { %>
                    <asp:HiddenField Value="<%# BindItem.Extras[i].ExtraProductId %>" ID="ExtraProductId" runat="server"  />
                    <asp:Label Text="<%# BindItem.Extras[i].ExtraName %>" ID="Name" runat="server" />
                    <asp:TextBox Text="<%# BindItem.Extras[i].ExtraQuantity %>" ID="Quantity"  runat="server" />
            <% } %> 
        </EditItemTemplate>
    </asp:FormView>
</div>

注意我用一个for循环替换了中继器,该循环遍历了使用用于访问每个Extra的索引的集合.这类似于我在ASP.NET MVC中需要做什么.当提交表单时,索引将与Web表单的其余部分一起发布,从而允许模型绑定器重建对象的有序列表.

我希望这是一些帮助,并原谅我的任何错误,因为我没有一个Web表单项目来测试这一刻.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读