0

For my job, i need to develop an web application in order to display a list of the jobs of some bank applications. Actually, the biggest part of this application have been done but I have to include a drop down list allowing to select the type of a job. I'm totally blocked on this point.

On my Index page, I have a list where some applications and their status (Job OK, KO, ...) are listed, the user can clic on the status (which is an image) of an application to display an other page where a list more detailed is displayed containing some informations in function of 2 parameters : application and id of the bank. Here is the link in my view :

<a href="@Url.Action("PageDomaines", new { Controller = "Suivi", CR = VarCR, Appli = VarAppli[j] })">
<img src="../../Content/Images/feu_rouge.png" alt="Statut OK" border="0" /></a>

The 2 parameters CR and Appli are passed to the URL from my View to the good action, after that, I do a query on them to get the list.

The problem is when I select a value in my drop down list located in the view of the action "PageDomaiens", the parameters are not kept, displaying an empty list. I'v done some test like verify if the value selected in my drop down list is send to my controller and it works, I can display the value selected in the view of "PageDomaines" but i don't keep my parameters CR and Appli...

Here is my action "PageDomaines" with my queries :

public ActionResult PageDomaines(string CR, string Appli, string DDL)
    {

        string GetCR = CR;
        string GetAppli = Appli;
        string GetDDL = DDL;


        if (GetDDL == null)
        {
            var items = GetDomaines();
            int Var1 = DateTime.Now.Year;
            int Var2 = DateTime.Now.Month;
            string Var3 = "" + Var1 + Var2;

            var Query = (from i in items
                         where i.Field<String>("CD_APPLI") == GetAppli && i.Field<String>("CD_CR") == GetCR && i.Field<Int64>("PERIODE").ToString().Contains(Var3)
                         select new Suivi { CD_TRT = i.Field<String>("CD_TRT"), LB_TRT = i.Field<String>("LB_TRT"), CD_CR = i.Field<String>("CD_CR"), PERIODE = i.Field<Int64>("PERIODE"), CD_APPLI = i.Field<String>("CD_APPLI"), STATUT = i.Field<String>("STATUT") }).ToList();

            ViewData["CR"] = GetCR;
            ViewData["Appli"] = GetAppli;

            return View(Query);
        }
        else
        {
            var items = GetDomaines();
            int Var1 = DateTime.Now.Year;
            int Var2 = DateTime.Now.Month;
            string Var3 = "" + Var1 + Var2;
            ViewData["DDL"] = GetDDL;
            var Query = (from i in items
                         where i.Field<String>("CD_APPLI") == GetAppli && i.Field<String>("CD_CR") == GetCR && i.Field<Int64>("PERIODE").ToString().Contains(Var3) && i.Field<String>("CD_TRT").Trim() == GetDDL
                         select new Suivi { CD_TRT = i.Field<String>("CD_TRT"), LB_TRT = i.Field<String>("LB_TRT"), CD_CR = i.Field<String>("CD_CR"), PERIODE = i.Field<Int64>("PERIODE"), CD_APPLI = i.Field<String>("CD_APPLI"), STATUT = i.Field<String>("STATUT") }).ToList();

            ViewData["CR"] = GetCR;
            ViewData["Appli"] = GetAppli;

            return View(Query);
        }
    }

    private List<DataRow> GetDomaines()
    {

        List<DataRow> liste = null;

        string query = "select CD_TRT, LB_TRT, CD_CR, PERIODE, CD_APPLI, case "
        + "when (exists (select 1 from dbo.LOGS l, dbo.REF_JOB j  where j.CD_JOB = l.CD_JOB and j.CD_TRT = t.CD_TRT and l.CD_CR = cr.CD_CR and l.STATUT = 'OK' and j.JR_EXEC >= DAY(l.DT_DEB))) "
        + "and not exists (select 1 from dbo.LOGS l, dbo.REF_JOB j  where j.CD_JOB = l.CD_JOB and j.CD_TRT = t.CD_TRT and l.CD_CR = cr.CD_CR  and l.PERIODE = p.PERIODE "
        + "and ((j.JR_EXEC < DAY(l.DT_DEB) and l.DT_DEB is not null) or (l.STATUT in('KO','NEXEC')))) then 'OK' "
        + "when (exists(select 1 from dbo.LOGS l, dbo.REF_JOB j  where j.CD_JOB = l.CD_JOB and j.CD_TRT = t.CD_TRT and l.CD_CR = cr.CD_CR  and l.PERIODE = p.PERIODE and j.JR_EXEC < DAY(l.DT_DEB) "
        + "and DAY(l.DT_DEB) IS not null and l.STATUT = 'KO')) then 'KO' "
        + "when (exists(select 1 from dbo.LOGS l, dbo.REF_JOB j  where j.CD_JOB = l.CD_JOB and j.CD_TRT = t.CD_TRT and l.CD_CR = cr.CD_CR  and l.PERIODE = p.PERIODE "
        + "and j.JR_EXEC < DAY(l.DT_DEB) and DAY(l.DT_DEB) IS not null and l.STATUT in('OK','NEXEC'))) then 'EN COURS' "
        + "when (exists(select 1 from dbo.LOGS l, dbo.REF_JOB j  where j.CD_JOB = l.CD_JOB and j.CD_TRT = t.CD_TRT and l.CD_CR = cr.CD_CR  and l.PERIODE = p.PERIODE "
        + "and j.JR_EXEC < DAY(l.DT_DEB) and DAY(l.DT_DEB) IS not null)) then 'EN RETARD' else 'NON INITIE' end STATUT "
        + "from dbo.REF_TRT t, dbo.REF_CR cr, (select distinct PERIODE from dbo.LOGS) p ";

        string connString = "Data Source=.;Initial Catalog=SUIVI_DWH;Integrated Security=True";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand objCommand = new SqlCommand(query, conn))
            {
                objCommand.CommandType = CommandType.Text;
                DataTable dt = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter(objCommand);
                conn.Open();
                adp.Fill(dt);
                if (dt != null)
                {
                    liste = dt.AsEnumerable().ToList();
                }
            }
        }

        return liste;
    }

    private List<DataRow> GetPERIODE()
    {
        List<DataRow> liste = null;

        string query = "select distinct PERIODE from dbo.LOGS";

        string connString = "Data Source=.;Initial Catalog=SUIVI_DWH;Integrated Security=True";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand objCommand = new SqlCommand(query, conn))
            {
                objCommand.CommandType = CommandType.Text;
                DataTable dt = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter(objCommand);
                conn.Open();
                adp.Fill(dt);
                if (dt != null)
                {
                    liste = dt.AsEnumerable().ToList();
                }
            }
        }

        return liste;
    }

And the view of "PageDomaines" :

@model IEnumerable<SUIVI_DWH.Models.Suivi>
@using SUIVI_DWH.Helpers
@{
    ViewBag.Title = "PageDomaines";
}
<h2>@ViewData["CR"]
    @ViewData["Appli"]
    @ViewData["DDL"]
</h2>
@using (Html.BeginForm("PageDomaines", "Suivi"))
{
    @Html.DropDownList("DDL", new SelectList(new[] { "MMCR", "MMCL", "MMCO", "MMSE", "MMAS" }), "--Select one--", new { onchange = "this.form.submit();" })
}

<table>
    <tr>
        <th>
            CD_TRT
        </th>
        <th>
            LB_TRT
        </th>
        <th>
            PERIODE
        </th>
        <th>
            STATUT
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @item.CD_TRT
            </td>
            <td>
                @item.LB_TRT
            </td>
            <td>
                @item.PERIODE
            </td>
            <td>
                @if (item.STATUT == null)
                {
                    <img src="../../Content/Images/feu_croix.png" alt="Statut null" />
                }
                else if (item.STATUT == "KO")
                {
                    <a href="@Url.Action("PageLogs", new { Controller = "Suivi", CR = ViewData["CR"], Appli = ViewData["Appli"], TRT = item.CD_TRT, PERIODE = item.PERIODE })">
                        <img src="../../Content/Images/feu_rouge.png" alt="Statut KO" border="0" /></a>    
                }
                else if (item.STATUT == "OK")
                {
                    <a href="@Url.Action("PageLogs", new { Controller = "Suivi", CR = ViewData["CR"], Appli = ViewData["Appli"], TRT = item.CD_TRT, PERIODE = item.PERIODE })">
                        <img src="../../Content/Images/feu_vert.png" alt="Statut OK" border="0" /></a>                
                }
                else if (item.STATUT == "EN RETARD")
                {
                    <a href="@Url.Action("PageLogs", new { Controller = "Suivi", CR = ViewData["CR"], Appli = ViewData["Appli"], TRT = item.CD_TRT, PERIODE = item.PERIODE })">
                        <img src="../../Content/Images/feu_orange.png" alt="Statut en retard" border="0" /></a>                
                }
                else if (item.STATUT == "NON INITIE")
                {
                    <a href="@Url.Action("PageLogs", new { Controller = "Suivi", CR = ViewData["CR"], Appli = ViewData["Appli"], TRT = item.CD_TRT, PERIODE = item.PERIODE })">
                        <img src="../../Content/Images/feu_blanc.png" alt="Statut non initié" border="0" /></a>                
                }
                else if (item.STATUT == "EN COURS")
                {
                    <a href="@Url.Action("PageLogs", new { Controller = "Suivi", CR = ViewData["CR"], Appli = ViewData["Appli"], TRT = item.CD_TRT, PERIODE = item.PERIODE })">
                        <img src="../../Content/Images/feu_jaune.png" alt="Statut en cours" border="0" /></a>                
                }
            </td>
        </tr>
    }
</table>

I hope that i haven't forgot anything.

Do you have any suggestion to resolve this problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Genyuumaru
  • 15
  • 6
  • IF I understand correctly the new page should display the list of "Suivi" in "Query", have you checked to make sure "Query" actually contains results (sorry but eliminating the most obvious first (: ) – JTMon Oct 11 '12 at 13:05
  • And just as an aside: why not use a viewModel that contains the list of Suivi, Appli, CR and DDL like that you don't have to use viewData? – JTMon Oct 11 '12 at 13:07
  • I've checked if my query contains the correct result and it's the case. The problem is with the parameters CR and Appli which aren't kept, so when the query try to select my result where my CD_CR field is equals to my parameter CR, my query return nothing because my parameters contains nothing. – Genyuumaru Oct 11 '12 at 13:17
  • And for the viewModel instead of the viewData, is it really better ? I don't really know ASP.net because it's only been three weeks since I started working with. – Genyuumaru Oct 11 '12 at 13:21
  • What value does VarCR have? is it empty? – JTMon Oct 11 '12 at 13:24
  • check the following question as a good starting point to investigate viewModel vs viewData. http://stackoverflow.com/questions/3074839/asp-net-mvc-viewdata-and-view-model-best-practices In my projects it is as close to forbidden as it can be for anyone to use viewBag or viewData. – JTMon Oct 11 '12 at 13:32
  • Sorry, I have forget to say one thing, in my index page, I have a table like that : http://hpics.li/d5ef711. When I clic on the status images, I pass by URL the name of the CR corresponding. So, when I arrive on the "PageDomaines" page, VarCR equals to CE (in the exemple on the image) but when i select a value in my drop down list, the value in VarCR disappears. I hope that i've answered at your question. Thanks for the link above, i'll check that. – Genyuumaru Oct 11 '12 at 13:41

3 Answers3

0

I am going to answer here just because of space constraints :). If I understand correctly, on the index page you have the table on the left and when you clicked on ESPAS Centre-Est, it opened a new page with the table to the right with listing for CE ESPAS. Now you have a drop down at the top and when the user chooses one of the values in it, VarCR and VarAppli are empty? (It is almost quitting time here so if I did not get it right I will blame it on being tired (:)

If that is the case, you need to show us the code for the drop down, but before that, I think you should look at getting the user to make a choice by clicking in the table or at least ticking a check box/pressing a button placed at the beginning or end of each row. I would hate to get the user to make a selection by 2 different methods when everything else looks the same, but that is just me. In case you go with the selecting from a table, you can have the URL contain the values for CE and Appli like in the first table, in case you still want the drop down you have to look at using a select list as it helps you have a display value and a "backing" value where you can store additional information such CE and Appli values you can check it out here: http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.108).aspx

Hope this answers your question, if it is not clear just let me know, it is clear in my head but it may have come out jumbled :)

JTMon
  • 3,189
  • 22
  • 24
0

I don't have enough space so i'll answer here.

Yeah, it's approximately this, when the user click on the STATUS of ESPAS CE in the first table (on the left), two parameters (CR and Appli where, in this case, CR = CE and Appli = ESPAS) are passed to the URL from the view for an action of my controller "SuiviController" with this link : <a href="@Url.Action("PageDomaines", new { Controller = "Suivi", CR = VarCR, Appli = VarAppli[j] })"> <img src="../../Content/Images/feu_rouge.png" alt="Statut OK" border="0" /></a>

After that, my action get the two parameters : CR and Appli in order to do a query with this parameters in a where clause (the query is in my first post). The resut of my query is returned to the view of my action and display the second table on the second page (on the right) with the drop down list. An other thing, when the user is on the second page, the URL is like that :

http://localhost:56672/Suivi/PageDomaines?CR=CE%20%20%20%20%20%20%20%20&Appli=ESPAS

But when the user select a value in the drop down list, the page are refresh and i lost my parameters, the url become this :

http://localhost:56672/Suivi/PageDomaines

And I lost the parameters passed from the first page when i select a value in my drop down list, i don't know how to keep them...

The code of the drop down list is show in the first post but here is it :

    @using (Html.BeginForm("PageDomaines", "Suivi"))
{
    @Html.DropDownList("DDL", new SelectList(new[] { "MMCR", "MMCL", "MMCO", "MMSE", "MMAS" }), "--Select one--", new { onchange = "this.form.submit();" })
}

Am I clear ?


For the point approached in your second paragraph, i don't want to give to the user a choice like that, for my question i used a drop down list with a simple selection just in order to display in the second table all the lines where CD_TRT is equals to the selected value, so if i select MMCL, the line : MMCL / Client - DMCL / 20121001 / Status will be displayed. But it's not the drop down list that i would have in the end. The final drop down list will be a list where the user could selected the jobs executed in function of a date according on this format : January 2012, February 2012,... or for an other application (BO by example) 25 January 2012, 26 Juanary 2012, etc... The value which will be selected will depend of the application selected on the first page.

I think that's all, if you have any doubt, say me :)

And really, thanks for your help.

Genyuumaru
  • 15
  • 6
  • Try including a hidden field for each of the variables in the form (so after beginform and before dropdown would do) and see if that solves the problem.... and as I said it was almost quitting time so that is why I missed the dropdown code...... that is my story and I am sticking it to it :) – JTMon Oct 12 '12 at 08:25
  • Is this correct ? `@using (Html.BeginForm("PageDomaines", "Suivi")) { Html.Hidden("CR", ViewData["CR"]); Html.Hidden("Appli", ViewData["Appli"]); @Html.DropDownList("DDL", new SelectList(new[] { "MMCR", "MMCL", "MMCO", "MMSE", "MMAS" }), "--Select one--", new { onchange = "this.form.submit();" }) }`I don't know how to use this hidden field, I've checked some post but I don't really understand how to use it... Edit : Aaaah !!! Where is my indentation ? It's so ugly xD – Genyuumaru Oct 12 '12 at 08:43
  • So, I have tried some things but it's always the same result : global variable and table. During my research to resolve my problem, I have seen the cookies and application variable, these things could help me or not ? And create a new class where I'll put the value of my parameter in it, could it be efficient ? – Genyuumaru Oct 12 '12 at 14:48
0

Yeah, I've find the solution with the help of someone in another forum.

It was really simple, the only thing that i had to add was in the BeginForm, I had to add this :

@using (Html.BeginForm("PageDomaines", "Suivi", new { CR = @ViewData["CR"], Appli = @ViewData["Appli"] }))

In the BeginForm, I have, in order, my action, my controler and the parameters and their values that i want to passed to my controler.

During my research, it was a solution that I thought but i didn't know how to code it. With the right syntax, all is going well.

Genyuumaru
  • 15
  • 6