模型绑定到自定义 Class MVC
Model Binding to a Custom Class MVC
我正在尝试将模型绑定到自定义列表 class。但是,每当我检查控制器中的值时,它们都显示为默认值。
我有这个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;
}
和这个表格:
<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>
这是控制器中更新操作的方法存根:
public ActionResult UpdateItems(int TitleID, ItemsViewModel[] IVM, List<int> HideFromEcomm) {}
视图模型没有绑定,可能是我没有正确命名它们,这个模型没有被传递到视图中,所以我不能使用 Textboxfor。关于如何解决这个问题的任何建议,如果你有资源我可以在何处阅读更多关于 MVC 中的模型绑定器的信息,这也是一个奖励
Binder 仅适用于属性,不适用于字段。将 class 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; }
}
我正在尝试将模型绑定到自定义列表 class。但是,每当我检查控制器中的值时,它们都显示为默认值。
我有这个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;
}
和这个表格:
<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>
这是控制器中更新操作的方法存根:
public ActionResult UpdateItems(int TitleID, ItemsViewModel[] IVM, List<int> HideFromEcomm) {}
视图模型没有绑定,可能是我没有正确命名它们,这个模型没有被传递到视图中,所以我不能使用 Textboxfor。关于如何解决这个问题的任何建议,如果你有资源我可以在何处阅读更多关于 MVC 中的模型绑定器的信息,这也是一个奖励
Binder 仅适用于属性,不适用于字段。将 class 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; }
}