ASP.NET MVC模型绑定在同一页相关的实体 [英] ASP.NET MVC Model Binding Related Entities on Same Page

查看:199
本文介绍了ASP.NET MVC模型绑定在同一页相关的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已经快把我逼疯了几个小时了......

This problem has been driving me crazy for several hours now...

在我的域名,我有2个被彼此相关的 SKU 项目实体。每个SKU可以有很多的项目。

In my domain, I have 2 entities that are related to each other Sku and Item. Each sku can have many items.

public class Sku
{
    private readonly EntitySet<Item> items;
    public Sku()
    {
        items = new EntitySet<Item>(AttachItems, DetachItems);
    }
    public int SkuId { get; set; }
    public string LongDescription { get; set; }
    public EntitySet<Item> Items
    {
        get { return items; }
        set{ items.Assign(value);}
    }
    private void AttachItems(Item entity)
    {
        entity.Sku = this;
    }
    private static void DetachItems(Item entity)
    {
        entity.Sku = null;
    }
}

public class Item
{
    public Sku Sku { get; set; }
    public int ItemId { get; set; }
    public string Category { get; set; }
    public string Description { get; set; }
}

我建立一个网页,将允许终端用户同时更新的SKU和对每个项目的某些字段某些字段

I am building a page that will allow the end-user to update some fields on the sku and some fields on each item at the same time.

<% using (Html.BeginForm("Save", "Merchant", FormMethod.Post, 
       new { enctype = "multipart/form-data" })) { %>       
<fieldset>
    <legend>Sku</legend>
    <p><label for="SkuId">SkuId:</label>
       <%= Html.TextBox("SkuId", Model.SkuId, 
           new{@readonly="readonly",onfocus="this.blur();"}) %></p>
    <p><label for="LongDescription">LongDescription:</label>
       <%= Html.TextBox("LongDescription", Model.LongDescription) %></p>
</fieldset>
<% for (int i = 0; i < Model.Items.Count; i++) { %>       
<fieldset>
    <legend>Item</legend>
    <p><label for="ItemId">ItemId:</label>
       <%= Html.TextBox(string.Format("items[{0}].{1}", i, "ItemId"), 
           Model.Items[i].ItemId, 
           new { @readonly = "readonly", onfocus = "this.blur();" })%></p>
    <p><label for="Category">Category:</label>
       <%= Html.TextBox(string.Format("items[{0}].{1}", i, "Category"), 
           Model.Items[i].Category)%></p>
    <p><label for="Description">Description:</label>
       <%= Html.TextBox(string.Format("items[{0}].{1}", i, "Description"), 
           Model.Items[i].Description)%></p>
</fieldset>
<%} // for-loop %>
    <p><input type="submit" value="Save" /></p>
<%} // form %>

我有一些控制器code,它通过接受工作既是 SKU 的EntitySet <$的C $ C>项目,然后分配项目 SKU

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(Sku sku, EntitySet<Item> items)
    {
        if (sku != null)
        {
            if (items != null)
            {
                sku.Items.Assign(items);
            }
        }

        // save Sku to repository ...
        // return Details view ...
    }

这个工作,但是我已经注意到,它使通过 DefaultModelBinder 每个项目两趟除了为 SKU一趟。当 SKU 绑定,为项目设置器调用,粘结剂甚至通过水合产品收集与正确的价值观。然而,在调用 items.Assign Items.Count 0 。这就是为什么我要重新分配控制器code的项目。我期待的项目被转移到了项目收集的粘合剂。这将消除每件额外的行程,因为项目参数在我的控制方法可以去除。为什么不是这方面的工作?

This works, however I have noticed that it makes two trips through the DefaultModelBinder for each Item in addition to one trip for the Sku. When the Sku is bound, the setter for Items is called, and the binder even passes in a hydrated Items collection with the correct values. However, after the call to items.Assign, Items.Count is 0. This is why I have to re-assign the items in the controller code. I was expecting the items to be transferred over to the Items collection by the binder. This should eliminate the extra trip per item, since the items parameter on my controller method could be removed. Why isn’t this working?

推荐答案

您可能需要为此创建自定义的模型绑定?

You might need to create a custom model binder for this?

这篇关于ASP.NET MVC模型绑定在同一页相关的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆