【问题标题】:How would I create this EditorTemplate for a collection?我将如何为集合创建此 EditorTemplate?
【发布时间】:2010-12-14 18:10:06
【问题描述】:

所以我有一个自定义 ViewModel (CompanySalaryDataViewModel),上面有一组其他 ViewModel (PersonalIncomeViewModel)。 最终,我想要一个 CompanySalaryDataViewModel 的 EditorTemplate,它以某种方式消耗 PersonalIncomeViewModel 的 EditorTemplate,当它被传递回我的 POST 控制器时,它提供了与我的 ViewModel 的双向绑定。强>

例如,假设它是这样的:

public class CompanySalaryDataViewModel
{
    string CompanyName { get; set; }
    IList<PersonalIncomeViewModel> AllSalaryData { get; set; }
}

public class PersonalIncomeViewModel
{
    long UniqueID { get; set; }
    string PersonName { get; set; }
    int JanIncome { get; set; }
    int FebIncome { get; set; }
    int MarIncome { get; set; }
    int AprIncome { get; set; }
    int MayIncome { get; set; }
    int JunIncome { get; set; }
    int JulIncome { get; set; }
    int AugIncome { get; set; }
    int SepIncome { get; set; }
    int OctIncome { get; set; }
    int NovIncome { get; set; }
    int DecIncome { get; set; }
}

我希望以类似于以下标记但有效的方式完成此操作:

<%= Html.TextBoxFor(x => x.CompanyName) %>
    <table cellspacing="0">
        <thead>
            <tr class="t-grid-header">
                <th class="t-header">ID</th>
                <th class="t-header">Person</th>
                <th class="t-header">Jan</th>
                <th class="t-header">Feb</th>
                <th class="t-header">Mar</th>
                <th class="t-header">Apr</th>
                <th class="t-header">May</th>
                <th class="t-header">Jun</th>
                <th class="t-header">Jul</th>
                <th class="t-header">Aug</th>
                <th class="t-header">Sep</th>
                <th class="t-header">Oct</th>
                <th class="t-header">Nov</th>
                <th class="t-header">Dec</th>
            </tr>
        </thead>
    <%
    var isAlt = false;
    foreach (var salaryData in Model.AllSalaryData)
    {
        var salary = salaryData;
        if (isAlt)
        {%>
            <tr class="t-alt">
        <%
        }
        else
        {%>
            <tr>
        <%
        }

        isAlt = !isAlt;
        %>
                <td><%=Html.TextBoxFor(x => salary.UniqueID)%></td>
                <td><%=Html.TextBoxFor(x => salary.PersonName)%></td>
                <td><%=Html.TextBoxFor(x => salary.JanIncome, new {id = salary.PersonName + "_1", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.FebIncome, new {id = salary.PersonName + "_2", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.MarIncome, new {id = salary.PersonName + "_3", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.AprIncome, new {id = salary.PersonName + "_4", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.MayIncome, new {id = salary.PersonName + "_5", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.JunIncome, new {id = salary.PersonName + "_6", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.JulIncome, new {id = salary.PersonName + "_7", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.AugIncome, new {id = salary.PersonName + "_8", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.SepIncome, new {id = salary.PersonName + "_9", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.OctIncome, new {id = salary.PersonName + "_10", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.NovIncome, new {id = salary.PersonName + "_11", style = "width: 45px;"})%></td>
                <td><%=Html.TextBoxFor(x => salary.DecIncome, new {id = salary.PersonName + "_12", style = "width: 45px;"})%></td>
            </tr>
    <%
    }%>
    </table>

这段代码的问题是:

  1. 每个“一月”文本框都具有相同的名称(salary.JanIncome - 不包含任何将其与其他工资区分开来的内容)。每个月都有同样的问题。
  2. 可能是因为问题 #1,当我发帖时,这不会绑定回 ViewModel - 我的 AllSalaryData 集合为空。
  3. 所有这些似乎都不是最佳实践,应该有更好的方法来做到这一点。

最终,我想我需要更好地理解将模板与集合一起使用,但另一部分是将t-alt 类正确应用到适当的表格行的“逻辑”。

注意 #1:我以这种方式为与这些文本框交互的一些 javascript 内容设置 HTML ID。

注意 #2:我并没有真正使用工资数据。

【问题讨论】:

  • 好的,所以我找到了一个不理想但有效的函数解决方案。显然foreach 是问题所在,但for(var i=0; i &lt; xxx.Count; i++) 有效,因为索引号用于生成渲染控件的name。功能现在,但我想以正确的方式做到这一点。

标签: c# .net asp.net-mvc asp.net-mvc-2


【解决方案1】:

不清楚您的问题: 1.“ViewModels”是视图模型——至少从设计的角度来看。您是否打算能够“批量”更新集合/列表或您的回帖以发送相同对象类型的集合? 2. 假设您的模型 - PersonalIncome[ViewModel] 是可持久的。因此,您将一次编辑一条记录,而您将使用的编辑器模板将仅用于集合中的一项。

您能否详细说明意图,而不仅仅是您想做的事情 - 除了使用文本框创建“列表”视图之外,还有其他选择。

【讨论】:

  • 替代方案不是一种选择 - 业务要求是所有这些都可以一次编辑(有充分的理由)。我有另一个 AJAX 绑定的网格,它正在执行 add'l ajax 查找以将数据插入到所有这些文本框中的数据并执行 javascript 数学运算,但我需要能够为最终用户提供最终的处理能力- 编辑最终数字。这个动态生成的“文本框网格”只有大约 10 行。然而,输入的选择量非常大。专注于我提出的问题,但是你可以忽略所有这些。
【解决方案2】:

好的,所以我找到了一个不理想但有效的函数解决方案。显然foreach 是问题所在,但for(var i=0; i &lt; xxx.Count; i++) 有效,因为索引号用于生成呈现控件的名称。不理想,但效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-08
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多