【发布时间】:2012-02-18 00:13:31
【问题描述】:
我对 MVC 还很陌生,我真的很想习惯模型绑定。我有一个以表格形式创建的简单模型。但是,当我只发布该表单时,文本框值会传递到控制器。我还需要使用 DisplayTextFor 完成的描述字段。这是我必须制作自定义模型绑定器的东西吗?我可以走捷径,只是将描述设置为没有边框的只读文本框,因此它看起来像文本,但我想以正确的方式进行。这是我的代码:
public class FullOrder
{
public List<FullOrderItem> OrderList { get; set; }
public string account { get; set; }
public string orderno { get; set; }
}
public class FullOrderItem
{
public int? ID { get; set; }
public int? OrderId { get; set; }
public string Description { get; set; }
public int Qty { get; set; }
public decimal? Price { get; set; }
}
这里是视图
<table class="ExceptionAltRow">
<tr style="background-color: #DDD;">
<td class="strong" style="width:500px;">
Description
</td>
<td class="strong" style="width:100px;">
Qty
</td>
<td class="strong" style="width:100px;">
Previous Purchases
</td>
</tr>
@for (int i = 0; i < Model.FullOrder.OrderList.Count(); i++)
{
<tr>
<td>
@Html.DisplayTextFor(m => m.FullOrder.OrderList[i].Description)
</td>
<td>
@Html.TextBoxFor(m => m.FullOrder.OrderList[i].Qty, new { @style = "width:50px;" })
</td>
</tr>
}
</table>
这里是控制器:
[HttpPost]
public ActionResult AddItem(FullOrder f)
{
//doesn't work description is not passed but qty is
}
有没有一种方法可以让我的模型只是简单地传递帖子上的描述,即使它不是我模型中绑定项目的文本框?
【问题讨论】:
标签: c# asp.net-mvc-3 entity-framework binding model