模型绑定到自定义类MVC

时间:2015-01-06 16:28:20

标签: c# asp.net-mvc asp.net-mvc-4 model model-binding

我试图将模型绑定到自定义类的列表。但是,当我检查控制器中的值时,它们都显示为默认值。

我有这个班级

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;
}

以及此表格:

<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>

这是控制器中Update操作中的Method stub:

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

视图模型没有绑定,我可能没有正确命名它们,这个模型没有被传递到视图中所以我不能使用Textboxfor。关于如何解决这个问题的任何建议,如果你有资源我可以在MVC中阅读更多有关模型绑定器的资源,也可以获得奖励

1 个答案:

答案 0 :(得分:1)

Binder仅适用于属性,而不适用于字段。将您的班级ItemsViewModel更改为用户属性而不是字段:

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; }
}