0

This is my model:

public class RegistryItemModel
{
    public string uuid { get; set; }
    public int version { get; set; }
    public string form { get; set; }
    public int formVersion { get; set; }
    public string modified { get; set; }
    public string nodeUUID { get; set; }
    public List<RegistryItemValue> data { get; set; }
}

I only need to update data prop. Here's the class:

public class RegistryItemValue
{
    public string id { get; set; }
    public string type { get; set; }
    public string label { get; set; }
    public string value { get; set; }
    public string key { get; set; }
    public string valueID { get; set; }
}

And then there's the view:

<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    @model SWG_ArtaAPI.Models.RegistryItemModel
    @using (Html.BeginForm("Save", "RegistryItem", FormMethod.Post))
    {
        <div class="form-group">
            <h3>@ViewBag.Title</h3>
            <table border="0">
                @for (var i = 0; i < Model.data.Count; i++)
                {
                    <tr>
                        @if (@Model.data[i].label != null)
                        {
                            <td><label>@Model.data[i].label</label></td>
                        }
                        @switch (@Model.data[i].type)
                        {
                            case "textbox":
                                <td>@Html.TextBoxFor(m => m.data[i].value)</td>
                                break;
                        }
                    </tr>
                }
            </table>
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Отправить" class="btn btn-default" />
            </div>
        </div>
    }
</body>
</html>

The view displays my data just fine, I get values in input fields and can change them. But when I try to save it (submit) in my post method, I get my model class with all props set to null. Here's how it's defined:

    [HttpPost]
    public string Save(RegistryItemModel model)

What is wrong with my code? Why I'm always getting null? Here's how it looks

Ivan Maslov
  • 168
  • 2
  • 13
  • You can create a EditorTemplate for the `RegistryValues` so model binds correctly on the post action. – Scrobi Aug 11 '17 at 07:40
  • 1
    Using `EditorTemplates` for `RegistryItemModel` properties with model binding should work. Also usage of `foreach` can be substituted by `for` loop with textbox index. – Tetsuya Yamamoto Aug 11 '17 at 07:43
  • And the code you have shown will not even compile. Show your real code –  Aug 11 '17 at 07:44
  • Fixed typos in the view code – Ivan Maslov Aug 11 '17 at 07:51
  • And what is that `switch` statement? If the value if not "textbox" then you would get non-consecutive indexers and binding would not work. Use a view model and pass only the values that you want to edit to the view. –  Aug 11 '17 at 07:53
  • @TetsuyaYamamoto you mean I should use EditorForModel? Could you link me an article that suits my example? – Ivan Maslov Aug 11 '17 at 07:56
  • @StephenMuecke Model.data[i].type can be textbox, listbox, date, numeric, textarea and link. How else can I do that? For each type I must have it's own input – Ivan Maslov Aug 11 '17 at 07:59
  • Then I assume your real code includes more `case` statements? But all this suggest a flaw with your design. –  Aug 11 '17 at 08:01
  • @IvanMaslov EditorForModel != EditorTemplates (one is abbreviation of EditorFor & another is predefined template). As you're probably using `@model IEnumerable` in the view, you can see this reference: https://stackoverflow.com/questions/11167538/saving-multiple-objects-from-mvc-view/11174133 or https://stackoverflow.com/questions/11487019/asp-net-mvc-editortemplate-for-ienumerable. – Tetsuya Yamamoto Aug 11 '17 at 08:01
  • @StephenMuecke for now I included only textbox – Ivan Maslov Aug 11 '17 at 08:05
  • And if the first `RegistryItemValue` in your collection is not `type="textbox"`, then you will be generating indexers which do not start at zero (and will not be bound by the `DefaultModelBinder`) –  Aug 11 '17 at 08:12
  • @StephenMuecke yes, now I see, if index doesn't interrupt, then I can get model in my controller. But if there isn't a textbox, there must be no input. How can I get around that? – Ivan Maslov Aug 11 '17 at 08:18
  • You can add a `` in your loop (before the `switch`) to make the collection bind. But its clear your design is wrong and its probably that you need to fix :) –  Aug 11 '17 at 08:21

0 Answers0