【问题标题】:Renderpartial or renderaction渲染部分或渲染动作
【发布时间】:2010-04-30 21:34:03
【问题描述】:

有一个产生有效职位空缺的行动。代码如下;

public ViewResult OpenVacancy() { var openvacancies = db.GetActiveVacancies(); 返回视图(空缺职位); }

我想在几个页面上使用这个列表,所以我想最好使用 html.renderaction(如果我在这里错了,请纠正我)。

请注意,视图和 .ascx 控件位于一个区域中。

然后,我通过在操作内部右键单击创建了一个视图,并创建了一个 .ascx 和一个强类型的 Vacancy 视图。我选择了“列表”的视图内容。

然后我将此行添加到所需的页面;

请注意,视图和 .ascx 控件位于一个区域中。

我得到的错误是;

找不到类型或命名空间名称“Vacancy”(您是否缺少 using 指令或程序集引用?)

.ascx 代码如下;

>" %>
<table>
    <tr>
        <th></th>
        <th>
            VacancyID
        </th>
        <th>
            JobTitle
        </th>
        <th>
            PositionID
        </th>
        <th>
            LocationID
        </th>
        <th>
            JobDescription
        </th>
        <th>
            JobConditions
        </th>
        <th>
            Qualifications
        </th>
        <th>
            RequiredSkills
        </th>
        <th>
            Certifications
        </th>
        <th>
            AdvertDate
        </th>
        <th>
            AdvertExpiryDate
        </th>
        <th>
            Status
        </th>
        <th>
            StaffLevel
        </th>
        <th>
            LineManagerEmail
        </th>
        <th>
            ApprovalFlag
        </th>
        <th>
            RequisitionDate
        </th>
    </tr>

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.ActionLink("Edit", "Edit", new { id=item.VacancyID }) %> |
            <%= Html.ActionLink("Details", "Details", new { id=item.VacancyID })%> |
            <%= Html.ActionLink("Delete", "Delete", new { id=item.VacancyID })%>
        </td>
        <td>
            <%= Html.Encode(item.VacancyID) %>
        </td>
        <td>
            <%= Html.Encode(item.JobTitle) %>
        </td>
        <td>
            <%= Html.Encode(item.PositionID) %>
        </td>
        <td>
            <%= Html.Encode(item.LocationID) %>
        </td>
        <td>
            <%= Html.Encode(item.JobDescription) %>
        </td>
        <td>
            <%= Html.Encode(item.JobConditions) %>
        </td>
        <td>
            <%= Html.Encode(item.Qualifications) %>
        </td>
        <td>
            <%= Html.Encode(item.RequiredSkills) %>
        </td>
        <td>
            <%= Html.Encode(item.Certifications) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.AdvertDate)) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.AdvertExpiryDate)) %>
        </td>
        <td>
            <%= Html.Encode(item.Status) %>
        </td>
        <td>
            <%= Html.Encode(item.StaffLevel) %>
        </td>
        <td>
            <%= Html.Encode(item.LineManagerEmail) %>
        </td>
        <td>
            <%= Html.Encode(item.ApprovalFlag) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.RequisitionDate)) %>
        </td>
    </tr>

<% } %>

</table>

<p>
    <%= Html.ActionLink("Create New", "Create") %>
</p>

【问题讨论】:

    标签: asp.net-mvc-2 renderpartial renderaction areas


    【解决方案1】:

    首先,如果你只是想在多个页面上重复使用你的视图,你应该使用一个共享的局部视图(将它放在共享文件夹中) 。

    其次,根据您的代码 sn-p,您的局部视图似乎不是强类型的。而不是

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    

    你会想要这样的:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Vacancy>>" %>
    

    【讨论】:

      最近更新 更多