ASP.Net MVC编辑器不工作

Cra*_*aig 1 asp.net asp.net-mvc

我想,我正在尝试使用EditorFor和partialview来渲染表格.

我有一个模型,其List <>属性定义如下:

public List<TransactionSplitLine> TransactionSplitLines { get; set; }
Run Code Online (Sandbox Code Playgroud)

这个想法是用户选择一些下拉菜单并在编辑框中输入一个值,然后单击一个按钮.模型返回到控制器,控制器将输入的值添加到List <>

[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);
}
Run Code Online (Sandbox Code Playgroud)

忽略CreateModel.它只是设置了一些数据.另外,我正在硬编码数据.这最终将来自某些形式值.

然后将模型返回到同一屏幕,允许用户收集更多数据.读取List <>中的任何项目并呈现表格.我还必须将当前监听项目值存储在隐藏字段中,以便可以将它们与输入的新数据一起提交回来,以便每次用户添加数据时列表都可以增长.

视图定义如下:

<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>
Run Code Online (Sandbox Code Playgroud)

这是我第一次尝试使用EditorFor ...

我的视图位于"Views/BankAccount/AccountTransaction.aspx"文件夹中

我在Views/Shared/TransactionSplitLines.ascx中创建了一个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>
Run Code Online (Sandbox Code Playgroud)

这是数据

'这是数据'只是测试内容,永远不会呈现.

当我运行它时,所有发生的事情是我的输出呈现为:

<table width="600">
    <thead>
        <tr class="headerRow">

            <td>
                Category
            </td>
            <td>
                Sub Category
            </td>
            <td>
                Amount
            </td>
        </tr>

    </thead>
    <tbody>
        Test Category
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

似乎ascx没有被使用?我希望看到'这是数据'文本.但是,没有.希望你能看到一个明显的错误?

Dar*_*rov 6

您的编辑器模板应该是:

~/Views/Shared/EditorTemplates/TransactionSplitLine.ascx
Run Code Online (Sandbox Code Playgroud)

要么:

~/Views/BankAccount/EditorTemplates/TransactionSplitLine.ascx
Run Code Online (Sandbox Code Playgroud)

ascx的名称始终是集合项的类型名称(TransactionSplitLine而不是TransactionSplitLines),它应位于~/Views/Shared/EditorTemplates~Views/ControllerName/EditorTemplates.

或者,如果要使用自定义编辑器模板名称:

<%= Html.EditorFor(m=>m.TransactionSplitLines, "~/Views/foo.ascx") %>
Run Code Online (Sandbox Code Playgroud)

或者UIHintAttribute在您的模型上使用.