Jim*_*mmy 8 c# asp.net-mvc nested-class model-binding
当我尝试创建一个新的'Flow'类时,嵌套类('Action')总是在控制器中返回null
所以我在类中有这样的类:
public class Flow
{
private Action actionField
private string nameField
private bool enabledField
...
}
public class Action
{
private ActionSchedule actionScheduleField
private ActionParameter actionParameterField
private nameField
}
public class ActionSchedule
...
Run Code Online (Sandbox Code Playgroud)
并为"流量"创建一个单独的视图
@model ProjectZeus.Models.Flow
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.name, new { @placeholder = "Flow name" })
@Html.ValidationMessageFor(model => model.name)
@Html.LabelFor(model => model.enabled)
@Html.EditorFor(model => model.enabled)
@Html.ValidationMessageFor(model => model.enabled)
@Html.Partial("FlowAction")
...
Run Code Online (Sandbox Code Playgroud)
然后是每个子类的部分视图
@model ProjectZeus.Models.FlowAction
@Html.TextBoxFor(model => model.name, new { @placeholder = "Action name" })
...
Run Code Online (Sandbox Code Playgroud)
我已经尝试创建类的实例并调用视图 - 错误,
我已经尝试在视图中创建类的实例 - 错误,
我尝试过不使用PartialViews:
@Html.TextBoxFor(model => model.action.name, new { @placeholder = "Action name" })
Run Code Online (Sandbox Code Playgroud)
我用谷歌搜索和谷歌googledddd但没有运气,请帮忙!?
编辑:
实施客户模型绑定器似乎有点矫枉过正.这个页面描述了同样的问题,但解决方案代码不会为我编译''当前上下文中不存在名称'helper''?- http://danielhalldev.wordpress.com/2013/08/23/partial-views-and-nested-mvc-model-binding/
EDIT2:
为简洁起见,我更改了模型定义 - 模型实际上是从xsd自动生成的:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class D53ESBFlow
{
private D53ESBFlowAction actionField;
[Required]
private string nameField;
...
private bool enabledField;
/// <remarks/>
public D53ESBFlowAction action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
Run Code Online (Sandbox Code Playgroud)
编辑3(凹凸):
看起来'binder'是创建属性而不是类对象?

我在 MVC 5、.NET 4.5、Visual Studio 2013 中也遇到了类似的问题。
这对我有用:添加一个构造函数,以便实例化所包含的类,使它们像 AntoineLev 所说的那样成为属性(而不是变量),然后将类添加到 Binding 中:
public class Flow
{
public Action actionField {get; set; }
public class Flow()
{
actionField = new Action(); // otherwise it shows up as null
}
}
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,在绑定中添加整个类:
public ActionResult Create([Bind(Include="action,name,enabled")] Flow flow)
{
...
}
Run Code Online (Sandbox Code Playgroud)
你的旅费可能会改变。
}
| 归档时间: |
|
| 查看次数: |
14339 次 |
| 最近记录: |