Razor 中的 MVC2 代码转换

发布于 2024-10-08 09:31:42 字数 800 浏览 3 评论 0原文

我正在使用某人的代码进行分页。他的代码位于 MVC 2 中,我希望它位于 MVC3 Razor 中。问题出在语法上。

下面是 mvc 中的代码,需要有人修复 razor 的语法。

问题出在这一行

IList<Customer> customers = (IList<Customer>)Model.Data;

//不能直接使用Model.Data。不选择泛型类型。

IList<Customer> customers = (IList<Customer>)Model.Data;
  foreach (Customer item in customers) { %>
<tr onclick="onRowClick(<%= item.ID %>)">
  <td>
    <%= Html.ActionLink("Edit", "Edit", new { id=item.ID}) %> |
    <%= Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
  </td>

  <td>
    <%= Html.Encode(item.ID) %>
  </td>

  <td>
    <%= Html.Encode(item.FirstName) %>
  </td>
</tr>
<% } %>

I am using someones code for paging. His code is in MVC 2 and I want it in MVC3 Razor. Problem is with syntax.

Below is the code in mvc need someone to fix syntax for razor please.

Problem is in this line

IList<Customer> customers = (IList<Customer>)Model.Data;

//Can't use Model.Data directly. Doesn't pick up generic type.

IList<Customer> customers = (IList<Customer>)Model.Data;
  foreach (Customer item in customers) { %>
<tr onclick="onRowClick(<%= item.ID %>)">
  <td>
    <%= Html.ActionLink("Edit", "Edit", new { id=item.ID}) %> |
    <%= Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
  </td>

  <td>
    <%= Html.Encode(item.ID) %>
  </td>

  <td>
    <%= Html.Encode(item.FirstName) %>
  </td>
</tr>
<% } %>

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

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

发布评论

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

评论(1

慢慢从新开始 2024-10-15 09:31:43

该行可以这样翻译:

@ {
    IList<Customer> customers = (IList<Customer>)Model.Data;
}

然后:

@foreach (Customer item in customers) {
    <tr onclick="onRowClick(@item.ID)">
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID })
            @:|
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>

        <td>
            @item.ID
        </td>

        <td>
            @item.FirstName
        </td>
    </tr>
}

我也将从这次迁移中受益,以改进此代码。目前,您在视图中使用的循环很丑陋,可以用显示模板替换。

因此,在您的主视图中:

@Html.DisplayFor(x => x.Data)

以及在 ~/Views/Home/DisplayTemplates/Customer.cshtml 中:

@model YourApp.Models.Customer
<tr onclick="onRowClick(@Model.ID)">
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID })
        @:|
        @Html.ActionLink("Delete", "Delete", new { id = Model.ID })
    </td>
    <td>
        @Model.ID
    </td>
    <td>
        @Model.FirstName
    </td>
</tr>

The line could be translated like this:

@ {
    IList<Customer> customers = (IList<Customer>)Model.Data;
}

and then:

@foreach (Customer item in customers) {
    <tr onclick="onRowClick(@item.ID)">
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID })
            @:|
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>

        <td>
            @item.ID
        </td>

        <td>
            @item.FirstName
        </td>
    </tr>
}

I would also benefit from this migration to improve this code. Currently you are using loops in your views which are ugly and could be replaced with display templates.

So, in your main view:

@Html.DisplayFor(x => x.Data)

and in ~/Views/Home/DisplayTemplates/Customer.cshtml:

@model YourApp.Models.Customer
<tr onclick="onRowClick(@Model.ID)">
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID })
        @:|
        @Html.ActionLink("Delete", "Delete", new { id = Model.ID })
    </td>
    <td>
        @Model.ID
    </td>
    <td>
        @Model.FirstName
    </td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文