c# - 如何在 ASP MVC 和 Entity Framework 中访问另一个 Controller 的 View

标签 c# visual-studio-2010 asp.net-mvc-3 entity-framework

我正在使用 C# 和 SQL Server 2005 开发 ASP .Net MVC 3 应用程序。

我也在使用 Entity Framework 和 Code First 方法。

我有一个 View 索引,它使用 Entity Framework 自动生成(其他 View 在一个文件夹中创建、编辑、删除...)。

这些 View 的文件夹名称是“ProfileGa”,填充它的 Controller 是“ProfileGaController”,模型是“FlowViewModel”。

我的问题是我想从“ProfileGaController”的 View 索引访问其他 Controller 的 View 。

解释更多: explication

那么,我如何通过操作链接从 Profile_Ga(准确地说是索引)的 View 访问 Gamme 的 View 。

我会放Index的代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>
<%@ Import Namespace="Helpers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Gestion de flux de production</h2>



<p>
    <%: Html.ActionLink("Ajouter une nouvelle gamme", "Create") %>
</p>

<table>
    <tr>
        <th>
            ID Gamme
        </th>
        <th>
            Entrée
        </th>
        <th>
           Sortie
        </th>
        <th>
            Gamme Suivante
        </th>
        <th>
            Etat
        </th>
        <th>Opérations</th>
    </tr>

<% foreach (var item in Model.Profile_GaItems) { %>
    <tr>
         <td>
            <%: Html.DisplayFor(modelItem => item.ID_Gamme) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.In_Ga) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Out_Ga) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Next_Gamme) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Etat) %>
        </td>
        <td>
            <%: Html.ActionLink("Modifier", "Edit", new { id=item.ID_Gamme }) %> |
            <%: Html.ActionLink("Détails", "Details", new { id=item.ID_Gamme }) %> |
            <%: Html.ActionLink("Supprimer", "Delete", new { id=item.ID_Gamme }) %>
        </td>
    </tr>


<% } %>

</table>
<% using (Html.BeginForm("Save", "ProfileGa"))
   { %>
  <div><%:Html.Label("Gamme :")%><%: Html.DropDownListFor(model => model.SelectedProfile_Ga, new SelectList(Model.Profile_GaItems, "ID_Gamme", "ID_Gamme"))%> <input type="button" value="Configurer" id="btnShowGestion" /></div> 




<div id="divGestion"><%: Html.Partial("Gestion", Model) %></div>
       <% } %>   
        <script type="text/javascript">

            $(document).ready(function () {





                $('#btnShowGestion').click(function () { $('#divGestion').slideToggle("slow") });



            });

</script>
<fieldset>
<legend>Gestion des Gammes</legend>
<table>
    <tr>
        <th>
            ID Gamme
        </th>
        <th>
            ID Poste
        </th>
        <th>
           Nombre de Passage
        </th>
        <th>
            Position
        </th>
        <th>
            Poste Précédent
        </th>
         <th>
            Poste Suivant
        </th>
        <th>Opérations</th>
    </tr>

<% foreach (var item in Model.GaItems) { %>
    <tr>
         <td>
            <%: Html.DisplayFor(modelItem => item.ID_Gamme) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.ID_Poste) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Nbr_Passage) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Position) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Last_Posts) %>
        </td>
         <td>
            <%: Html.DisplayFor(modelItem => item.Next_Posts) %>
        </td>
        <td>
            <%: Html.ActionLink(?????????????????) %> |
            <%: Html.ActionLink(?????????????????) %> |
            <%: Html.ActionLink(?????????????????) %>
        </td>
    </tr>


<% } %>
</table>
 </fieldset>     
</asp:Content>

注意: 这 '????????'在 ActionLink 中,这是我想要访问 Gamme View 的地方。

最佳答案

这是 ActionLink 扩展方法 ( from MSDN ) 的签名之一:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues)

第三个参数 controllerName 就是你想要的。

因此,在您的情况下:

Html.ActionLink("Modifier", "Edit", "Gamme" new { id=item.ID_Gamme })

您还提到您“想要访问另一个 Controller 的 View ”,这不是 ActionLink 方法的确切含义,此方法只会发出带有适当 Url 的 a 标签(链接)将加载请求的 [Controller.Action]。

关于c# - 如何在 ASP MVC 和 Entity Framework 中访问另一个 Controller 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16645247/

相关文章:

c# - Visual Studio 中的程序集引用问题

asp.net-mvc - 从 ModelMetaData 获取另一个属性的值

c# - 在发送到 Controller/DB 之前编码 HTML

asp.net-mvc - 添加 View 对话框 MVC3

C# Razor 页面 BindProperty 属性不起作用

c# - 当 T 是接口(interface)时,为什么我的从 T 到 SomeStruct<T> 的转换运算符不工作?

c# - 我如何为文本字段生成动态 ID 并从 asp.net 4 mvc3 中的模型数组获取数据

枚举的 c# 替代方案以获得智能感知优势

c++ - Visual Studio 2010 SP1 中的断言和未使用的变量

visual-studio-2010 - Visual Studio 2010 中的 SourceSafe 错误 - 无法编辑文件 - 当前用户已在不同位置 checkout 文件