Home Ask Login Register

Developers Planet

Your answer is one click away!

Nicholas Magnussen February 2016

MVC Post with complex types, model is empty

I have the following code to fill my model before posting it to my controller. The list I'm iterating through is a list.

<div class="tab-content">
            @*@foreach (var description in Model.Category.C_CategoryDescription)*@
            @for (var i = 0; i < Model.Category.C_CategoryDescription.Count; i++)
                <div id="@("tab" + @Model.Category.C_CategoryDescription.ToList()[i].ProductTypeId)" class="@(Model.Category.C_CategoryDescription.ToList()[i] == @Model.Category.C_CategoryDescription.First() ? "tab-active" : "tab")">
                    <div class="form-group ">
                        @Html.LabelFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionTop, "Beskrivelse - Top", htmlAttributes: new {@class = "control-label col-md-2"})
                        <div class="col-md-10">
                            @Html.TextAreaFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionTop, new {@class = "richText"})

                    <div class="form-group">
                        @Html.LabelFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionBottom, "Beskrivelse - Bund", htmlAttributes: new {@class = "control-label col-md-2"})
                        <div class="col-md-10">
                            @Html.TextAreaFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionBottom, new {@class = "richText"})

The HTML comes out fine. But as soon as I catch the post in my controller, the model is empty. Not NULL, but empty. I read numerous articles saying that it points to a problem with the model binding.

I changed my code to reflect what's described:


Stephen Muecke February 2016

Your code for generating the elements in the collection needs to be

@Html.TextAreaFor(m => m.Category.C_CategoryDescription[i].DescriptionTop, new {@class = "richText"})

which will generate the correct name attributes

<textarea name="Category.C_CategoryDescription[0].DescriptionTo" ... />
<textarea name="Category.C_CategoryDescription[1].DescriptionTo" ... />

Your current use a .ToList() is generating incorrect name attributes (not tested, but I assume its name="[0].DescriptionTo")

Alternatively you can use a custom EditorTemplate for the C_CategoryDescription model if you cannot change the collection to implement IList

In Views/Shared/EditorTemplates/C_CategoryDescription.cshtml (note the name of the file must match the name of the class)

@model yourAssembly.C_CategoryDescription

<div class="form-group ">
    @Html.LabelFor(m => m.DescriptionTop, "Beskrivelse - Top", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextAreaFor(m => m.DescriptionTop, new { @class = "richText" })

and then in the main view, to generate the correct html for each item in the collection

@Html.EditorFor(m => m.Category.C_CategoryDescription)

Post Status

Asked in February 2016
Viewed 2,203 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved