locked
List status by line RRS feed

  • Question

  • User1670624291 posted

    in my project I have a problem identical to the one I reported here https://forums.asp.net/t/2174818.aspx?Sum+values+by+state+and+in+the+same+list, but this time they are the result of a search.

    I currently have this function, but the problem is that when filtering it only reads the Staus value of the first line does not go through the rest

    if the first status has a different value the result is returned in white

    Classes:

    public class OrdersColorsViewModel
        {
            public Programa order { get; set; }
            public List<ColorsAndQuantities> colers { get; set; }
            public Malha malha { get; set; }
            public Preco_Malha preco { get; set; }
            public List<Programa_Malha> promalha { get; set; }
        }

    public class ColorsAndQuantities
        {
            public Programa_Cor coler { get; set; }
            public List<Programa_Cor_Info> quant { get; set; }
            public List<Programa_Cor_Info_Status> status { get; set; }
        }

    models:

    public partial class Programa
        {
            public int ID_Programa { get; set; }
            public string Num_Encomenda { get; set; }
            public Nullable<int> Ref_Balu { get; set; }
            public Nullable<System.DateTime> Semana_Embarque { get; set; }
            public string Modelo { get; set; }
        }

    public partial class Programa_Cor
        {
            public int ID_Programa { get; set; }
            public int ID_Programa_Malha { get; set; }
            public int ID_Linha_Cor { get; set; }
            public string Cor { get; set; }
        }

    public partial class Programa_Cor_Info
        {
            public int ID_Programa { get; set; }
            public int ID_Linha_Cor { get; set; }
            public int ID_Programa_Malha { get; set; }
          public Nullable<int> Cliente { get; set; }
      }
    public partial class Programa_Cor_Info_Status
        {
            public int ID_Programa { get; set; }
            public int ID_Linha_Cor { get; set; }
            public int ID_Programa_Malha { get; set; }
            public Nullable<int> Talao { get; set; }
            public string Status { get; set; }
            public string Obs { get; set; } 
        }

    to perform the filtering I use a dropdownlist to pass a value to my string startStatus 

    in my controller:

    return View(result.Where(x => x.colers[0].status[0].Status == startStatus));

    in view

    i use the code below to validate the filtering

    @using (Html.BeginForm("Index", "Home", FormMethod.Get))
        {
            <div class="float-left" style="margin-left:20px">
                @Html.TextBox("searchString", ViewBag.CurrentFilter as string, new { @class = "form-control mr-sm-2", @placeholder = "Procurar", style = "width: 300px;" })
            </div>
            <div class="float-right">
                &nbsp Status: @Html.DropDownList("startStatus", "Todos") &nbsp &nbsp
    
                &nbsp Data de Embarque: @Html.DropDownList("startData", "Todas") &nbsp &nbsp
            </div>
        }

    I present my items Status this way:

    @if (item2.status.Count != 0)
                        {
                            <td style="width: 80px;">
                                @foreach (var item4 in item2.status)
                                {
                                    @Html.HiddenFor(m => @item4.ID_Programa, new { @class = "ID_Programa" })
                                    @Html.HiddenFor(m => @item4.ID_Linha_Cor, new { @class = "ID_Linha_Cor" })
                                    @Html.HiddenFor(m => @item4.ID_Programa_Malha, new { @class = "ID_Programa_Malha" })
                                    @Html.HiddenFor(m => @item4.ReturnDate, new { @class = "Data_mod" })
    
                                    @Html.TextBoxFor(m => @item4.Talao, new { @class = "Talao form-control form-control-sm font-weight-bold", disabled = true, style = "width: 80px;" })
                                }
                            </td>
                            <td id="Status">
                                @foreach (var item4 in item2.status)
                                {                              
                                    @Html.DropDownListFor(m => @item4.Status, new List<SelectListItem>
                                             {
                                           new SelectListItem { Text = @item4.Status, Value = @item4.Status, Selected = true},
                                           new SelectListItem { Text = "Sem malha", Value = "Sem malha"},
                                           new SelectListItem { Text = "No gabinete (dos moldes)", Value = "No gabinete (dos moldes)"},
                                           new SelectListItem { Text = "A testar corte", Value = "A testar corte"},
                                           new SelectListItem { Text = "A bordar", Value = "A bordar"},
                                           new SelectListItem { Text = "Para cortar", Value = "Para cortar"},
                                           new SelectListItem { Text = "Em corte", Value = "Em corte"},
                                           new SelectListItem { Text = "A estampar", Value = "A estampar"},
                                           new SelectListItem { Text = "Para colocar", Value = "Para colocar"},
                                           new SelectListItem { Text = "Em confeção", Value = "Em confeção"},
                                           new SelectListItem { Text = "Para aparar", Value = "Para aparar"},
                                           new SelectListItem { Text = "Para lavar", Value = "Para lavar"},
                                           new SelectListItem { Text = "Para tingir", Value = "Para tingir"},
                                           new SelectListItem { Text = "Para ferros", Value = "Para ferros"},
                                           new SelectListItem { Text = "Para arranjos", Value = "Para arranjos"},
                                           new SelectListItem { Text = "Para embalagem", Value = "Para embalagem"},
                                           new SelectListItem { Text = "Embalado", Value = "Embalado"},
                                           new SelectListItem { Text = "Para controle", Value = "Para controle"},
                                           new SelectListItem { Text = "Aguarda aprovação cliente", Value = "Aguarda aprovação cliente"},
                                               }, new { style = "width: 180px;", @class = "Status form-control form-control-sm font-weight-bold", disabled = true })
                                }
                            </td>
                            <td id="Obs">
                                @foreach (var item4 in item2.status)
                                {
                                    @Html.TextAreaFor(m => @item4.Obs, new { @class = "Obs form-control form-control-sm font-weight-bold", disabled = true, style = "width: 400px;", rows = "1" })
                                }
                            </td>
                            <td style="text-align:right;">
                                <button class="btn ToEditbtn btn btn-outline-success btn-sm"><span aria-hidden="true"></span>Editar</button>
                                <button class="btn Editbtn btn btn-outline-success btn-sm" style="display:none"><span aria-hidden="true">Guardar</span></button>
                                <p style="font-size:10px;">
                                    @foreach (var item4 in item2.status)
                                    {
                                        @item4.Data_mod
                                    }
                                </p>
                            </td>
                        }

    I tried to create something with the same structure, but in the end I have my result as a list

    var test = result.Select(m => m.colers.Select(c => c.status.Where(n => n.Status == startStatus).ToList()
                ).ToList());

    in my viewI tried

    viewbag.stringstatus = startStatus;
    @if( viewbag.stringstatus != null ){
          @item4.Status.Where(i => i == viewbag.stringstatus)
    }

    thanks for all the tips

    Friday, March 5, 2021 5:49 PM

Answers

  • User1686398519 posted

    Hi MiguelMi, 

    I am sorry for the late reply this time. I spent a lot of time to read the code you provided.

    I modified the code you provided according to your needs.

    1. Regarding querying based on the value of Status:
      1. I found your query logic is very complicated.
      2. As far as I know, you only need to add query conditions when the value of startStatus is not empty.
      3. The same is true for searchString and startData.
    2. About editing the value of Status:
      1. If you need to use EF to modify your data, the Programa_Cor_Info_Status table needs to have a primary key.
      2. I added a new field(ID_Programa_Cor_Info_Status) and set it as the primary key.

    You can check the modified code below.

    Controller

            public ActionResult Index(string searchString, string startData, string startStatus, string empty = "", bool emb = false, int idOne = 1)
            {
                 ... ...
                if (!String.IsNullOrEmpty(searchString))//if searchString has value , add query conditions 
                {
                    string upper = searchString.ToUpper();
                    proResult = proResult.Where(x => x.order.Modelo.Contains(upper) || x.order.Colecao.Contains(upper) || x.order.Num_Encomenda.Contains(upper) || x.order.Ref_Cliente.Contains(upper) || x.order.Cod_Artigo.Contains(upper) || x.malha.Ref_Malha.Contains(upper) || x.malha.Nome_Malha.Contains(upper));
                }
                if (!String.IsNullOrEmpty(startData))//if startData has value , add query conditions 
                {
                    DateTime startttt = Convert.ToDateTime(startData, cultureinfo);                   
                    proResult = proResult.Where(x => x.order.Semana_Embarque == startttt);
                }
                if (!String.IsNullOrEmpty(startStatus))//if startStatus has value , add query conditions 
                {
                    proResult = proResult.Select(x => new OrdersColorsViewModel
                    {
                        order = x.order,
                        malha = x.malha,
                        preco = x.preco,
                        promalha = x.promalha,
                        colers = x.colers.Where(c=>c.status.Any(k=>k.Status== startStatus)).ToList()
                    }).Where(m=>m.colers.Count()!=0);
                }
                return View(proResult.ToList());
    
            }
            [HttpPost]
            public JsonResult EditPost(Programa_Cor_Info_Status statusData)
            {
                var status = db.Programa_Cor_Info_Status.Where(m => m.ID_Programa_Cor_Info_Status==statusData.ID_Programa_Cor_Info_Status).FirstOrDefault();
                status.Talao = statusData.Talao;
                status.Status = statusData.Status;
                status.Obs = statusData.Obs;
                status.Data_mod = statusData.Data_mod;
                db.SaveChanges();
                return Json(status, JsonRequestBehavior.AllowGet);
            }

    Index

         ... ...
         @Html.HiddenFor(m => @item3.ID_Programa, new { @class = "ID_Programaf" })
         @Html.HiddenFor(m => @item3.ID_Linha_Cor, new { @class = "ID_Linha_Corf" })
         ... ...
         status.ID_Programa_Cor_Info_Status = $(this).closest('tr').find(".ID_Programa_Cor_Info_Status").val();
         ...  ...

    Here is the result. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 18, 2021 10:03 AM

All replies

  • User1686398519 posted

    Hi MiguelMi, 

    when filtering it only reads the Staus value of the first line does not go through the rest

    if the first status has a different value the result is returned in white

    What you mean is that when you filter the data based on the status, the result (drop-down list) is incorrect (as shown in the picture).

    To be honest, your model is very complicated, and you only provided part of the code, but I cannot reproduce your problem. If so, can you explain it in more detail or provide the necessary detailed code?

    Best Regards,

    YihuiSun

    Monday, March 8, 2021 6:12 AM
  • User1670624291 posted

    i share more code for anyone who can help me

    and share github with the complete code of my controller and view

    https://github.com/miguelBalu/projectbalu

    Tuesday, March 9, 2021 9:30 AM
  • User1686398519 posted

    Hi MiguelMi, 

    If it is convenient for you, can you give your test data?

    Best Regards,

    YihuiSun

    Thursday, March 11, 2021 9:02 AM
  • User1670624291 posted

    I tried something like this, but it gives the same result:

    ViewBag.stringstatus = startStatus;
    @foreach (var item4 in item2.status.Where(i => i.Status == ViewBag.stringstatus))
                                {
       \\more code
    }

    Friday, March 12, 2021 9:01 AM
  • User1686398519 posted

    Hi MiguelMi, 

    I am sorry for the late reply this time. I spent a lot of time to read the code you provided.

    I modified the code you provided according to your needs.

    1. Regarding querying based on the value of Status:
      1. I found your query logic is very complicated.
      2. As far as I know, you only need to add query conditions when the value of startStatus is not empty.
      3. The same is true for searchString and startData.
    2. About editing the value of Status:
      1. If you need to use EF to modify your data, the Programa_Cor_Info_Status table needs to have a primary key.
      2. I added a new field(ID_Programa_Cor_Info_Status) and set it as the primary key.

    You can check the modified code below.

    Controller

            public ActionResult Index(string searchString, string startData, string startStatus, string empty = "", bool emb = false, int idOne = 1)
            {
                 ... ...
                if (!String.IsNullOrEmpty(searchString))//if searchString has value , add query conditions 
                {
                    string upper = searchString.ToUpper();
                    proResult = proResult.Where(x => x.order.Modelo.Contains(upper) || x.order.Colecao.Contains(upper) || x.order.Num_Encomenda.Contains(upper) || x.order.Ref_Cliente.Contains(upper) || x.order.Cod_Artigo.Contains(upper) || x.malha.Ref_Malha.Contains(upper) || x.malha.Nome_Malha.Contains(upper));
                }
                if (!String.IsNullOrEmpty(startData))//if startData has value , add query conditions 
                {
                    DateTime startttt = Convert.ToDateTime(startData, cultureinfo);                   
                    proResult = proResult.Where(x => x.order.Semana_Embarque == startttt);
                }
                if (!String.IsNullOrEmpty(startStatus))//if startStatus has value , add query conditions 
                {
                    proResult = proResult.Select(x => new OrdersColorsViewModel
                    {
                        order = x.order,
                        malha = x.malha,
                        preco = x.preco,
                        promalha = x.promalha,
                        colers = x.colers.Where(c=>c.status.Any(k=>k.Status== startStatus)).ToList()
                    }).Where(m=>m.colers.Count()!=0);
                }
                return View(proResult.ToList());
    
            }
            [HttpPost]
            public JsonResult EditPost(Programa_Cor_Info_Status statusData)
            {
                var status = db.Programa_Cor_Info_Status.Where(m => m.ID_Programa_Cor_Info_Status==statusData.ID_Programa_Cor_Info_Status).FirstOrDefault();
                status.Talao = statusData.Talao;
                status.Status = statusData.Status;
                status.Obs = statusData.Obs;
                status.Data_mod = statusData.Data_mod;
                db.SaveChanges();
                return Json(status, JsonRequestBehavior.AllowGet);
            }

    Index

         ... ...
         @Html.HiddenFor(m => @item3.ID_Programa, new { @class = "ID_Programaf" })
         @Html.HiddenFor(m => @item3.ID_Linha_Cor, new { @class = "ID_Linha_Corf" })
         ... ...
         status.ID_Programa_Cor_Info_Status = $(this).closest('tr').find(".ID_Programa_Cor_Info_Status").val();
         ...  ...

    Here is the result. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 18, 2021 10:03 AM
  • User1670624291 posted

    thanks again, for the help, it works perfect

    to see if I understand the structure, when performing the select I need to call all the models (order, colers ... etc) referring to the table I can't load just based on the colers?

    Thursday, March 18, 2021 2:18 PM