0
votes

I've an ASP.NET proj. MVC Core with a series of tables. Each has a field called "year". In the _Layout, I've a dropdown list with the next 5 years (2017, 2018, ect). I'd like to receive in every action of the controller the value of the dropdown selection in order to apply the filter "by year" to the query.

_LAYOUT.CSHTML PAGE

 <select name="EF_DROP" id="EF_DROP" class="btn btn-default" style="width:105px;" aria-haspopup="true" aria-expanded="true" type="button">
                                <option value="0">EF 2017</option>
                                <option value="1">EF 2018</option>
                                <option value="2">EF 2019</option>
                                <option value="3">EF 2020</option>
                            </select>
//this is my Jquery to set Hidden Fields
 $(function () {
             $("#EF_DROP").on("change", function () {
                 $("#YEAR").val($(this).find(":selected").text().substr(3, 4));
             });
         });

         $(function () {
             $("#YEAR").val($("#EF_DROP").find(":selected").text().substr(3, 4));
         });

EACH VIEW PAGE

 <input type="hidden" asp-for="YEAR" />

THE PROBLEM IS ON THE CONTROLLER:

 public async Task<IActionResult> Index()
        {

            var applicationDbContext = _context.MYTABLE.Include(a => a.RELATE)
              .Where(x=>x.Year == **??**);
            return View(await applicationDbContext.ToListAsync());
        }

I've tried to add a Singleton but can't find the getValue from collection or with Request.Form["name"].... Someone can help me please? Thanks in advance.

2
Why don't you just use the year itself as the value? Like <option value="2017">EF 2017</option>Anderson Pimentel
What I can simplify in this mode? sorry for the beginner question!! but I'm not expert.Luca C.
This way you wouldn't even need to use the hidden field YEAR. Just rename EF_DROP to YEAR. You also won't need any jQuery code.Anderson Pimentel
Use inheritance, you need the year attribute in a high level in your class hierarchy so that it gets more visibility in lower level classes. Create a "BaseController" (Inherit from MVC Controller class) class and add "Year" as a public property in that BaseController. Inherit your MVC controller from the Base Controller. Porperty "Year" will be available in all controllers inherited by BaseController. "Year" will be accessible in your Razor view as well.Dhanuka777
Thanks!! I will try now!Luca C.

2 Answers

0
votes

I have solved with the following method :

_LAYOUT.CSHTML with JQuery

     $(function () {
             var value = localStorage.getItem("POS");

             if (value != null)
             {
                $("#EF_DROP").val(value).change();
             }
             else
             {
                 $("#EF_DROP").val(0).change();
             }
             $("#Year").val($("#EF_DROP").find(":selected").text().substr(3, 4));

                 $("#EF_DROP").on("change", function () {
                     $("#Year").val($(this).find(":selected").text().substr(3, 4)); //Hidden Field
                     var url = "/Home/SetEF";
                     $.post(url, { item: $(this).find(":selected").text().substr(3, 4) }, function (response) {
                         localStorage.setItem("POS", response);
                     });
                 });
         });

    </script>

CONTROLLER

 [HttpPost]
        public int SetEF(string item)
        {
            int pos;
            switch (item)
            {
                case "2017":
                    pos = 0;
                    TempData["EF"] = 2017;
                    return pos;

                case "2018":
                    pos = 1;
                    TempData["EF"] = 2018;
                    return pos;

                case "2019":
                    pos = 2;
                    TempData["EF"] = 2019;
                    return pos;

                case "2020":
                    pos = 3;
                    TempData["EF"] = 2020;
                    return pos;

                default:
                    return 0;
            }
 }

I use the TempData for retrieving data into another controller and so I can apply the filter.... anyway I think this isn't elegant but I don't know how do it in a new way.

0
votes

Use inheritance, you need the year attribute in a high level in your class hierarchy so that it gets more visibility in lower level classes.

Create a "BaseController" (Inherit from MVC Controller class) class and add "Year" as a public property in that BaseController. Inherit your MVC controller from the Base Controller. Porperty "Year" will be available in all controllers inherited by BaseController. "Year" will be accessible in your Razor view as well.