Binding MVC Models to Nested Classes

When I try to create a new class "Stream", nested classes ("Action") always return as null in the controller

So, I have classes inside classes:

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 ... 

And one view for the Stream view

 @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") ... 

and then partial views for each of the subclasses

 @model ProjectZeus.Models.FlowAction @Html.TextBoxFor(model => model.name, new { @placeholder = "Action name" }) ... 

I tried to instantiate the classes and call the view - error,

I tried creating class instances in the views themselves - error,

I did not try to use PartialViews:

 @Html.TextBoxFor(model => model.action.name, new { @placeholder = "Action name" }) 

I have googled and googled and googleedddd, but no luck, please help !?

Edit:

Implementing a client middleware model seems redundant. The same problem is described on this page, but the solution code will not compile for me. Helper "Name" does not exist in the current context? - http://danielhalldev.wordpress.com/2013/08/23/partial-views-and-nested-mvc-model-binding/

Edit2:

I changed the model definitions for short - the model is actually generated automatically from 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; 

Change 3 (hit):

Looks like 'binder'is is creating a property, not a class object? whereRu

+6
source share
4 answers

You forgot {get; set; } accessors in property names?

+3
source

I had a similar problem with MVC 5, .NET 4.5, Visual Studio 2013.

Here's what worked for me: add a constructor so that the contained class gets an instance, make it properties (and not variables) like AntoineLev, and add the class to Binding:

 public class Flow { public Action actionField {get; set; } public class Flow() { actionField = new Action(); // otherwise it shows up as null } } 

In your controller add the whole class to the binding:

 public ActionResult Create([Bind(Include="action,name,enabled")] Flow flow) { ... } 

Your mileage may vary.

}

+1
source

As a result, I received a response to the request and displayed all the properties individually by name:

 flow.action = new D53ESBFlowAction { name = Request["action.name"], ... 
0
source

I had similar troubles, and this article by Jimmy Bogar helped me. Article here

A look at the generated html will show that with a partial view, html does not include the name of the nested class, so by default it cannot bind it. Providing a binding operator, as in one of the answers above, solves the problem

0
source

Source: https://habr.com/ru/post/972082/


All Articles