我将如何为集合创建此 EditorTemplate?

发布于 2024-10-07 18:00:46 字数 4883 浏览 0 评论 0 原文

因此,我有一个自定义 ViewModel (CompanySalaryDataViewModel),其中包含其他 ViewModel 的集合 (PersonalIncomeViewModel)。 最终,我希望有一个用于 CompanySalaryDataViewModel 的 EditorTemplate,它以某种方式使用 PersonalIncomeViewModel 的 EditorTemplate,以便在以下情况下提供到我的 ViewModel 的 2 路绑定:它被传回我的 POST 控制器。

例如,假设它是这样的:

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 集合为 null。
  3. 所有这些看起来都非常非最佳实践,并且应该有更好的方法来做到这一点。

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

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

注意#2:我并不是真的用工资数据来做这件事。

So I have a custom ViewModel (CompanySalaryDataViewModel) that has a collection of other ViewModels on it (PersonalIncomeViewModel). Ultimately, I want to have an EditorTemplate for a CompanySalaryDataViewModel that consumes an EditorTemplate for a PersonalIncomeViewModel somehow in a way that it provides 2-way binding to my ViewModel when it's passed back into my POST controller.

For example, let's say it's something like this:

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; }
}

I want this done in a way where it's similar to the following markup but works:

<%= 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>

The problems with this code are:

  1. Each "January" textbox has the same name (salary.JanIncome - doesn't include anything to differentiate it from another salary). Same problem with every month.
  2. Perhaps because of problem #1, this doesn't bind back to the ViewModel when I post - my AllSalaryData collection is null.
  3. All of this seems VERY non-best-practice and like there should be a better way to do this.

Ultimately, I guess I need to better understand using templates with collections but the other part of it is the "logic" to properly apply the t-alt class to the appropriate table row.

NOTE #1: I set the HTML IDs this way for some javascript stuff I have interacting with these textboxes.

NOTE #2: I'm not really doing this with salary data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-10-14 18:00:46

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

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

Not clear with your question:
1. 'ViewModels' are view models - at least from design point of view. Is your intent to be able to 'batch' update the collection/list or your post back to send collection of the same object type?
2. Let's assume your model - PersonalIncome[ViewModel] is persistable. So, you will be editing one record at time and the editor template you will be using will be just for an item in the collection.

Can you elaborate the intent not just what you want to do - there could be other alternatives than creating a 'list' view with Textboxes.

请你别敷衍 2024-10-14 18:00:46

好吧,我找到了一个功能解决方案,虽然不理想,但它有效。显然 foreach 是问题所在,但 for(var i=0; i 有效,因为索引号用于生成名称呈现的控件。不理想,但效果很好。

Okay, so I have found a function solution that's not ideal but it works. Apparently the foreach is the problem but a for(var i=0; i < xxx.Count; i++) works because the index number gets used for generation of the name of the rendered control. Not ideal but this works perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文