user4071723
user4071723

Reputation:

Model Binding to a Custom Class MVC

I'm trying to do model binding to a list of a custom class. However when ever I check the values in the controller they all appear as their default values.

I have this class

public class ItemsViewModel
{
     public int ID;
     public decimal RoyaltyPercent = 0;
     public decimal ListPrice = 0;
     public int Parts = 0;
     public decimal RemitPercent = 0;
     public DateTime? DiscontinuedDate;
}

and this form:

<form action="@Href("~/Title/UpdateItems/")" method="post" class="form-horizontal" role="form">
<table class="table table-bordered table-condensed">
    <tbody>
        <tr>
            <th class="text-center">Format Code</th>
            <th class="text-center">Parts</th>
            <th class="text-center">List Price</th>
            <th class="text-center">ISBN</th>
            <th class="text-center">Discontinue Date</th>
            <th class="text-center">Hide from eComm</th>
            <th class="text-center">Date Created</th>
            <th class="text-center">Royalty %</th>
            <th class="text-center">Remit %</th>
            @*<th class="text-center hidden editable">Save</th>*@

        </tr>

        @foreach (var it in Model.Items)
        {


            <tr>
                <td class="hidden"><input type="hidden" name="ItemsViewIVMModel[@ItCount].ID" value="@it.ID"></td>
                <td class="text-center">
                    <span class="hidden editable"><input type="text" style="width:100%"  name="IVM[@ItCount].Parts" value="@it.NumOfParts.ToString()"/></span>
                </td>
                <td class="text-center">
                    <span class="hidden editable"><input type="text" style="width:100%" name="IVM[@ItCount].ListPrice" value="@string.Format("{0:N2}", it.ListPrice)"/></span>
                </td>
                <td class="text-center">
                    <span class="hidden editable"><input type="text" style="width:100%" value="@it.ISBN"/></span>
                </td>
                <td class="text-center"><span class="uneditable">@tDisDate</span>
                     <input type="text" style="width:100%" class="datepicker" name="IVM[@ItCount].DiscontinuedDate" value="@tDisDate"/></span>
                </td>
                <td class="text-center">
                    <span class="hidden editable"><input type="checkbox" @isChecked name="HideFromEcomm" value="@it.ID"></span>
                </td>
                <td class="text-center">@it.InsTimeStamp.ToString("MM/dd/yyyy")</td>
                <td class="text-center">
                    <span class="hidden editable"><input type="text" style="width:100%" name="IVM[@ItCount].RoyaltyPercent" value="@(it.RoyaltyPercent ?? 0.00m)"/></span>
                </td>
                <td class="text-center">
                    <span class="hidden editable"><input type="text" style="width:100%" name="IVM[@ItCount].RemitPercent" value="@(it.RemitPercent ?? 0.00m)"/></span>
                </td>
            </tr>

            ItCount++;
        }
    </tbody>
</table>
<div class="box-footer clearfix no-border hidden editable">
    <div class="btn-group pull-right">
        <button type="button" class="btn btn-default btn-flat close-toggle">Cancel</button>
        <button type="submit" class="btn btn-primary btn-flat">Save</button>
    </div>
</div>

this is the Method stub for the in the Update action in the controller:

 public ActionResult UpdateItems(int TitleID, ItemsViewModel[] IVM, List<int> HideFromEcomm) {}

The View Model isn't binding, is it possible that I'm not naming them correctly, This model is not being passed into the view so I cannot use Textboxfor. Any suggestions on how to fix this, also a bonus if you have resources on where I can read more about the model binder in MVC

Upvotes: 0

Views: 827

Answers (1)

dotnetom
dotnetom

Reputation: 24901

Binder only works with properties, not fields. Change you class ItemsViewModel to user properties instead of fields:

public class ItemsViewModel
{
     public int ID { get; set; }
     public decimal RoyaltyPercent { get; set; }
     public decimal ListPrice { get; set; }
     public int Parts { get; set; }
     public decimal RemitPercent { get; set; }
     public DateTime? DiscontinuedDate { get; set; }
}

Upvotes: 1

Related Questions