msc*_*d02 7 c# binding entity-framework model asp.net-mvc-3
我对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; }
}
Run Code Online (Sandbox Code Playgroud)
这是视图
<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>
Run Code Online (Sandbox Code Playgroud)
这是控制器:
[HttpPost]
public ActionResult AddItem(FullOrder f)
{
//doesn't work description is not passed but qty is
}
Run Code Online (Sandbox Code Playgroud)
有没有办法,我可以让我的模型只是简单地传递一个帖子上的描述,即使它不是我的模型绑定项目的文本框?
将发布到您的应用程序的唯一数据是正在提交的表单上可用的数据(当然,除非表单字段被禁用)。您可以通过实现自定义模型绑定器来覆盖控制器所看到的内容。
在这种情况下,您的表单由单个文本字段的许多实例组成:
@Html.TextBoxFor(m => m.FullOrder.OrderList[i].Qty, new { @style = "width:50px;" })
Run Code Online (Sandbox Code Playgroud)
如果您想要填写描述和其他内容,则需要将它们显示在表格上。如果它们不需要可见,那么您可以使用 HiddenFor 帮助器:
@Html.HiddenFor(m => m.FullOrder.OrderList[i].Description)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2836 次 |
最近记录: |