gpt4 book ai didi

c# - 为什么 MVC 不绑定(bind)我的嵌套集合?

转载 作者:行者123 更新时间:2023-11-30 17:20:59 25 4
gpt4 key购买 nike

如果我尝试通过表单编辑几个 linqTOsql 对象。

首先是 Stream 类:

[Table]
public class Stream
{
[HiddenInput(DisplayValue = false)]
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public long StreamID { get; set; }

/*other properties removes for brevity*/

// relationship (one stream to many Stream2FieldTypes)
// uses EntitySet<Stream2FieldTypes> and OtherKey for the FK in Stream2FieldTypes
private EntitySet<Stream2FieldTypes> _Stream2FieldTypes;
[System.Data.Linq.Mapping.Association(Storage = "_Stream2FieldTypes", OtherKey = "StreamID")]
public EntitySet<Stream2FieldTypes> Stream2FieldTypes
{
get { return this._Stream2FieldTypes; }
set { this._Stream2FieldTypes.Assign(value); }
}

}

然后是 Stream2FieldTypes 类:

[Table]
public class Stream2FieldTypes
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public long s2fID { get; set; }

/*other properties removed for brevity*/

[Column]
public long StreamID { get; set; } // FK




// relationship (one Stream2FieldTypes to Streams)
private EntitySet<Stream> _Stream;
[Association(Storage = "_Stream", ThisKey = "StreamID")]
public EntitySet<Stream> Stream
{
get { return this._Stream; }
set { this._Stream.Assign(value); }
}
}

我使用 linqTOsql 存储库获取这些对象并将它们发送到 View ,如下所示:

    public ActionResult StreamEdit(long id)
{
Genesis.Domain.Entities.Stream stream = genesisRepository.Streams.FirstOrDefault(x => x.StreamID == id);

return View(stream);
}

一切都毫无问题地发送到 View ...但是当提交 View 上的表单时,Stream.Stream2FieldTypes 由于某种原因丢失了。我通过此操作捕获了表单提交:

    [HttpPost]
public ActionResult StreamEdit( long id, Genesis.Domain.Entities.Stream form)
{

return View(form);
}

并且该 View 没有 Stream2FieldTypes 集合。

View 正在像这样编写 Stream2FieldTypes:

<% for (int i = 0; i < (Model.Stream2FieldTypes != null ? Model.Stream2FieldTypes.Count() : 0); i++) %>
<% { %>
<div class="ListItem">
Label: <%: Html.EditorFor(x => x.Stream2FieldTypes[i].s2fLabel, new { style="width:100px" })%>
Required: <%: Html.EditorFor(x => x.Stream2FieldTypes[i].s2fIsRequired)%>
Field Type ID": <%: Html.EditorFor(x => x.Stream2FieldTypes[i].FieldTypeID, new { style="width:20px;" })%>
Stream ID: <%: Html.EditorFor(x => x.Stream2FieldTypes[i].StreamID) %>
</div>
<% } %>

为什么 mvc 中的默认模型绑定(bind)器不能正确绑定(bind)嵌套集合?

最佳答案

我建议您使用 POCO View 模型,而不是那些困惑的模型类。他们不适应这种观点,很难与之合作。尝试绑定(bind)到 LINQ 特定集合,如 EntitySet<T>这不是一个好主意。使用编辑器模板可能确实可行,但即使可行,请记住它应该是一个临时解决方案,直到您引入适当的 View 模型。

在您的主视图中用这个替换循环:

<%: Html.EditorFor(x => x.Stream2FieldTypes) %>

然后在 ~/Views/Home/EditorTemplates/Stream2FieldTypes.ascx 中添加强类型分部 View :

<%@ Control 
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Stream2FieldTypes>"
%>
<div class="ListItem">
Label:
<%: Html.EditorFor(x => x.s2fLabel, new { style="width:100px" }) %>

Required:
<%: Html.EditorFor(x => x.s2fIsRequired) %>

Field Type ID:
<%: Html.EditorFor(x => x.FieldTypeID, new { style="width:20px;" })%>

Stream ID:
<%: Html.EditorFor(x => x.StreamID) %>
</div>

通常这应该负责生成正确的输入名称,但为了避免人们将此作为一个很好的例子:我再次重复使用正确的 View 模型。

关于c# - 为什么 MVC 不绑定(bind)我的嵌套集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3843209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com