Search code examples
c#model-view-controllermvc-editor-templates

Custom EditorTemplate for complex object takes main object members instead of member object's member


I'm currently developing an interface for a system, and I'm facing a problem which seems strange to me.

I have a Command class, which looks like this:

public class Command
{
    public string Name { get; set; }
    public List<Parameter> Parameters { get; set; }
}

And the Parameter class is:

public class Parameter
{
    public string Name { get; set; }
    public string Value { get; set; } 
}

So I have created a view to edit the command and its arguments on the same page. I have an EditorTemplate for my class Parameter (in Views\Commander\EditorTemplates):

@model MyApplication.Models.Parameter

<div class="form-horizontal">
    <div class="form-group">
        @Html.LabelFor(model => model.Name)
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Value)
        <div class="col-md-10">
            @Html.EditorFor(model => model.Value)
        </div>
    </div>
</div>

And my Command editor view (in Views\Commander):

@model MyApplication.Models.Command

@using (Html.BeginForm())
{
    <div class="form-horizontal">
        <div class="form-group">
            @Html.LabelFor(model => model.Name)
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Parameters)
            <div class="col-md-10">
                @Html.EditorFor(model => model.Parameters)
            </div>
        </div>
    </div>

    <input type="submit" value="Save"/>
}

My problem is that my edit view takes Command.Name value and puts it in Parameter.Name field. It may be obvious for somebody, bud I really don't know what causes this. Can someone point me in the right direction ?

EDIT: Let's say my command is RunExe -path C:\FooBar.exe. When I open my Edit page for this command, instead of having path in my parameter name edit field, it shows RunExe.

And I still have RunExe in command name edit, and C:\FooBar.exe in parameter value edit.


Solution

  • I finally managed to solve my problem. I just had a typo when assigning value to my member which used the name of the command instead of th name of the parameter -_-'