1

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 : explication

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.

2 Answers 2

3

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].

Sign up to request clarification or add additional context in comments.

5 Comments

thx but the link takes me of the Edit of Profile_Ga, that's means no diffrence between this : Html.ActionLink("Modifier", "Edit", "Gamme" new { id=item.ID_Gamme }) and this : <%: Html.ActionLink("Modifier", "Edit", new { id=item.ID_Gamme }) %>
i saw your edit,,,so i have to not use ActionLink to access to another controller,,so what is the solution ?
sorry,,,I want that take me to the view 'Edit' in the Folder 'Gamme' of the controller 'GammeController'
Maybe i get you wrong. but, In MVC, Views are not meant to be accessed directly, only using Actions. please read the last paragraph in my answer or read more on how the MVC Framework works.
yes I read,,,i am just now need an approach how i put a link to take to the view of Edit of the Controller 'Gamme',,Is that Possible ?
2

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)

3 Comments

Then the first one will be your answer :)
I created a new view to that controller ,,,but I have some problems of null values,,,Hang On plz,,,I will give you link to the new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.