简体   繁体   中英

How to access to views of another Controller in ASP MVC and Entity Framework

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

I am using also Entity Framework and Code First Method.

I have a view Index which is generated automatically using Entity Framework (with others view create,edit,delete...in one folder).

The folder name of those views is 'ProfileGa' and the controller which populate its is 'ProfileGaController' and the model is 'FlowViewModel'.

My problem is I want to access to views of anothers controllers from the view Index of 'ProfileGaController'.

TO EXPLAIN MORE : 解释

So, How can I access to the views of Gamme from the view of Profile_Ga (precisly Index) with an action link.

I will put the code of 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>

Note : The '????????' in the ActionLink this is where I want to access to the views of Gamme.

This is one of the signatures of the ActionLink extension method ( from MSDN ):

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

The 3rd parameter controllerName is what you want here.

Thus, in your case:

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

You're also mentioning that you ' want to access the views ' of another Controller, that's not exactly what the ActionLink method is about, this method would simply issue an a tag (link) with the appropriate Url that will load the requested [Controller.Action].

You can also do that from the controller if you prefer, rather than a URL

using this line of code in the action method

return View("ViewName", model)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM