Search code examples
c#asp.net-coremodel-view-controllerasp.net-core-mvc

I can't get the value in my select form and use it in my Controller


The parameters dictionary contains a null entry for parameter 'yr1' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult SpesGraduate(Int32)' in 'SPESMS.Controllers.ReportsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Here is the View:

<form action="/spesms/Reports/SpesGraduate" method="get" >
     <select name="yr1" id="yr1">
          <option value="2019">2019</option>
          <option value="2020">2020</option>
          <option value="2021">2021</option>
          <option value="2022">2022</option>
      </select>
  </form>
<script>
        $(document).ready(function () {
            $('select').on('change', function () {
                $(this).closest('form').submit();
                location.reload();
            });
        });
    </script>

here is the controller

public ActionResult SpesGraduate(int yr1)
        {
            int poid = GlobalHelper.UserPO(Session["UserPO"].ToString());
            int implYear = (int)Session["ImplementYear"];
            int selyr1 = yr1;
        }

Solution

  • I can't get the value in my select form and use it in my Controller

    I have reproduced your issue in my side but not sure why you are getting the error, In my test I haven't get any error as yours. However, the reason for getting null parameter for using location.reload(); which reloading your page soon you submit the request. I have get rid of that line and its working as expected and I am getting the value accordingly.

    Controller:

    public ActionResult SpesGraduate(int yr1)
            {
                int poid = GlobalHelper.UserPO(Session["UserPO"].ToString());
                int implYear = (int)Session["ImplementYear"];
                int selyr1 = yr1;
            }
    

    Note: No change I made here just it as yours.

    View & Script:

    <script>
            $(document).ready(function () {
                $('select').on('change', function () {
                    $(this).closest('form').submit();
                    //location.reload(); 
                });
            });
        </script>
    

    Note: Everything would be same just ommit location.reload(); which reloading your page soon you submit request thus, you cannot see the impact.

    Output:

    enter image description here