ASP.Net MVC EditorFor不工作
我想呈现一个表,使用EditorFor和partialview,我想。ASP.Net MVC EditorFor不工作
我有一个模型与一个这样定义一个列表<>属性:
public List<TransactionSplitLine> TransactionSplitLines { get; set; }
的想法是,用户选择几个下拉菜单和输入一个值到编辑框,点击一个按钮。该模型可以追溯到控制器,控制器会将输入的值的列表<>
[HttpPost]
public ActionResult AccountTransaction(AccountTransactionView model)
{
var reply = CreateModel(model);
if (model.CategoryIds != null)
{
foreach (var c in model.CategoryIds)
{
reply.TransactionSplitLines.Add(new TransactionSplitLine { Amount = "100", Category = "Test Category", SubCategory = "Test More", CategoryId = int.Parse(c) });
}
}
reply.TransactionSplitLines.Add(new TransactionSplitLine { Amount = "100", Category = "Test Category", SubCategory = "Test More", CategoryId = 1 });
return View("AccountTransaction", reply);
}
忽略CreateModel。它只是设置一些数据。另外,我对数据进行了硬编码。这最终将来自某些形式价值。
该模型然后返回到相同的屏幕,允许用户输入更多的数据。读取列表<>中的任何项目并呈现表格。我还必须将当前的侦听项目值存储在隐藏字段中,以便可以将它们与输入的新数据一起提交回来,以便每次用户添加数据时都可以增加列表。
视图的定义是这样的:
<table width="600">
<thead>
<tr class="headerRow">
<td>
Category
</td>
<td>
Sub Category
</td>
<td>
Amount
</td>
</tr>
</thead>
<tbody>
<%=Html.EditorFor(m=>m.TransactionSplitLines) %>
</tbody>
</table>
这是我与EditorFor第一次尝试......
我的观点是在一个文件夹“视图/的BankAccount/AccountTransaction.aspx
我已创建在一个视图ASCX /共享/ TransactionSplitLines.ascx
用于ASCX的代码是这样的:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.TransactionSplitLine>" %>
<tr>
<td>
<%=Model.Category %>
<%=Html.HiddenFor(x => x.CategoryId)%>
</td>
<td>
<%=Model.SubCategory %>
<%=Html.HiddenFor(x => x.SubCategoryId)%>
</td>
<td>
<%=Model.Amount %>
<%=Html.HiddenFor(x => x.AmountValue)%>
</td>
</tr>
这是数据
“这就是数据”只是测试的东西,这是从来没有显示。
当我运行此,所发生的一切是我的输出呈现为:
<table width="600">
<thead>
<tr class="headerRow">
<td>
Category
</td>
<td>
Sub Category
</td>
<td>
Amount
</td>
</tr>
</thead>
<tbody>
Test Category
</tbody>
</table>
这似乎是ASCX没有被使用?我期望看到“这是数据”文本。但是,没有。希望你能看到明显的错误?
你的编辑模板应该是:
~/Views/Shared/EditorTemplates/TransactionSplitLine.ascx
或:
~/Views/BankAccount/EditorTemplates/TransactionSplitLine.ascx
的ASCX的名字始终是集合项目的类型名称(TransactionSplitLine
而不是TransactionSplitLines
),它应该位于~/Views/Shared/EditorTemplates
或~Views/ControllerName/EditorTemplates
。
或者,如果你想使用自定义编辑模板名称:
<%= Html.EditorFor(m=>m.TransactionSplitLines, "~/Views/foo.ascx") %>
或者在模型中使用UIHintAttribute
。
谢谢@Darin Dimitrov!这解决了它。我修正了ASCX的名称,它正在工作。谢谢。 – Craig 2011-02-19 00:08:32