Jer*_*ell 5 linq data-binding asp.net-mvc model-binding entityset
这个问题让我疯了好几个小时了......
在我的领域,我有2个是彼此相关的其他实体Sku和Item.每个sku可以有很多项目.
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; }
}
Run Code Online (Sandbox Code Playgroud)
我正在构建一个页面,允许最终用户同时更新sku上的某些字段和每个项目的某些字段.
<% 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 %>
Run Code Online (Sandbox Code Playgroud)
我有一些控制器代码,通过接受两者的工作原理Sku和EntitySet的Item,然后分配Items到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 ...
}
Run Code Online (Sandbox Code Playgroud)
这工作,但我注意到,它使得通过两趟DefaultModelBinder每个Item除了为一个行程Sku.当Sku绑定时,Items调用setter ,并且绑定器甚至通过Items具有正确值的水合集合.但是,在打电话之后items.Assign,Items.Count是0.这就是我必须重新分配控制器代码中的项目的原因.我期待Items通过活页夹将项目转移到集合中.这应该消除每个项目的额外行程,因为items我的控制器方法上的参数可以被删除.为什么这不起作用?
| 归档时间: |
|
| 查看次数: |
2454 次 |
| 最近记录: |