我正在使用 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 。
解释更多:
那么,我如何通过操作链接从 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/