locked
Reload a View after passing a value from View to Controller RRS feed

  • Question

  • User-1546622187 posted

    Hi, I have a ExampleView.cshtml in which a user can choose to see some charts related to :

    • a week
    • a month
    • an year

    So, in my View i have a "combo box" with values: Weekly, Monthly and Yearly. After click on "Apply" button, If the user has choosen "Weekly", he can see weekly charts, and so on. On the ExampleController.cs I have already implemented a switch statement based on a ViewBag.Flag value, which in default case it has value 0 (week) and in another cases it has value 1 (month) or 2 (year), which call some methods who load some data in order to get the right values to build the charts (bar chart, donut chart and line chart - I'm already able to build them). 

    So, I want to retrieve the selection done in the View and passing it to the Controller to load another data to populate the charts

    I hope you can unterstand my question.

    Have a nice day smile

    Thursday, October 17, 2019 10:29 AM

Answers

  • User475983607 posted

    So, I want to retrieve the selection done in the View and passing it to the Controller to load another data to populate the charts

    Is your actual question is how to post values to an action when a select changes?

    View

    @{
            ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <form method="post">
        <select id="period" name="period">
            <option value="0">-- Select --</option>
            <option value="day">Day</option>
            <option value="week">Week</option>
            <option value="month">Month</option>
        </select>
    </form>
    
    @section scripts {
        <script>
            $('#period').change(function () {
                $('form').submit();
            });
        </script>
    }

    Actions

            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }
    
    
            [HttpPost]
            public IActionResult Index(string period)
            {
                return Content(period);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 17, 2019 10:52 AM

All replies

  • User475983607 posted

    So, I want to retrieve the selection done in the View and passing it to the Controller to load another data to populate the charts

    Is your actual question is how to post values to an action when a select changes?

    View

    @{
            ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <form method="post">
        <select id="period" name="period">
            <option value="0">-- Select --</option>
            <option value="day">Day</option>
            <option value="week">Week</option>
            <option value="month">Month</option>
        </select>
    </form>
    
    @section scripts {
        <script>
            $('#period').change(function () {
                $('form').submit();
            });
        </script>
    }

    Actions

            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }
    
    
            [HttpPost]
            public IActionResult Index(string period)
            {
                return Content(period);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 17, 2019 10:52 AM
  • User-1546622187 posted

    Thank you a lot!

    Thursday, October 17, 2019 12:20 PM