Bilal
Bilal

Reputation: 182

List property doesn't bind on post in controller

I know this question has been asked a zillion time but pardon me as I have wasted quite a number of hours now, thus finally posting it. So to summarize I have a a view Model having some complex type prperties and a complex type list property .

public class ViewModel
{
    public ViewModel()
    {
        Gains = new List<Gain>();

    }
    [UIHint("Common")]
    public AllRegRecordsLog commonRegisterFields { get; set; }
    public Potential Potential { get; set; }
    [UIHint("Gains")]
    public List<Gain> Gains { get; set; }

    public void CreateGains(int count = 1)
    {

        for (int i = 0; i < count; i++)
        {

            Gains.Add(new Gain());

        }


    }

}

now my view createOrEdit I call this property as ,

@Html.EditorFor(model => model.Gains)

I have placed an editor template inside my specific view folder

@model Gains
    table id="gainsTable" class="content-tables"style="width:98%">
    <tr id="defaultGainsRow">
            <td class="editor-label">
                @Html.LabelFor(model => model.VolumeGainLow)
            </td>
            <td class="editor-field">
                @Html.TextBoxFor(model => model.VolumeGainLow)
                @Html.ValidationMessageFor(model => model.VolumeGainLow)
            </td>

            </tr>

     <tr>
    <td colspan="4">
      <input id="addGainsButton"type="button" class="t-button" Value="Add Potential Gains"/>
    </td>
    </tr>
    </table>

But somehow I get the error "The model item passed into the dictionary is of type 'System.Collections.Generic.List but expects Gains"

I am using asp.Net MVC 4

Please point me in right direction.

Thanks

Bilal

Upvotes: 1

Views: 210

Answers (3)

Bilal
Bilal

Reputation: 182

Thanks for assistance guys I solvex it using the following post

http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/

Upvotes: 1

smartcaveman
smartcaveman

Reputation: 42246

It looks like you are trying to declare your view as bound to a property instead of to a type. This is not possible. You have to declare the model as a valid type that is referenced by your web project in order for templating to work.

The type of the Gains property is List<Gain>. Since yourGainsview has aGainstype declared, yourUIHintAttribute` is invalid.

Upvotes: 0

Daniel Powell
Daniel Powell

Reputation: 8293

Modify the editortemplate to take a List and also perform the looping logic inside that

@model List<Gain>
@foreach(var gain in Model)
{
    table id="gainsTable" class="content-tables"style="width:98%">
    <tr id="defaultGainsRow">
            <td class="editor-label">
                @Html.LabelFor(gain=> gain.VolumeGainLow)
            </td>
            <td class="editor-field">
                @Html.TextBoxFor(gain=> gain.VolumeGainLow)
                @Html.ValidationMessageFor(gain=> gain.VolumeGainLow)
            </td>

            </tr>

     <tr>
    <td colspan="4">
      <input id="addGainsButton"type="button" class="t-button" Value="Add Potential Gains"/>
    </td>
    </tr>
    </table> 
}

EDIT: my razor might not be 100% but you get the idea

Upvotes: 0

Related Questions