locked
Data doesn't show in Index IActionResult RRS feed

  • Question

  • User-1408067692 posted

    So GetTools gets data from a select in HTML and it searches for data where the division is the same. It does finds it when debugging but when redirecting to the index something goes wrong. It doesn't show in my view. Only the divisions are showing, but the tools are not.

    The functions in my controller:

            public IActionResult Index(List<ToolDivision> tools)
            {
                var divisions = (from td in _context.ToolDivision select td.division).Distinct().ToList();
    
                var toolModel = new ToolModel()
                {
                    divisions = divisions,
                    tools = tools
                };
    
                return View(toolModel);
            }
    
            public IActionResult GetTools(string divisions)
            {
                var tools = _context.ToolDivision.Where(t => t.division == divisions).ToList();
    
                return RedirectToAction("Index", tools);
            }

    Code in my view:

        <form>
            <select name="divisions" id="divisions">
                @foreach (var division in @Model.divisions)
                {
                    <option value="@division">@division</option>
                }
            </select>
            <input asp-action="GetTools" type="submit" value="Tonen" name="divisions"/>
        </form>
    
        @foreach (var tool in Model.tools)
        {
            <p>@tool.tool</p>
        }

    Thanks!

    Monday, November 2, 2020 10:27 PM

All replies

  • User-474980206 posted

    redirect to action is expecting an object of route values, or a simple object

        new { param1 = "p1", param2 = "p2"} 

    it does not support a list, nor complex properties and especially the results of a database query.

    as the browser can not call the Index method except via ajax, not sure the point. just change GetTools to 

            public IActionResult GetTools(string divisions)
            {
                var tools = _context.ToolDivision.Where(t => t.division == divisions).ToList();
    
                return Index(tools);
            }

    Monday, November 2, 2020 11:09 PM
  • User711641945 posted

    Hi KasperMartens,

    You can't pass value from one action to another action by using RedirectToAction.

    You could use VIewBag or TempData...Check the following answer:

    https://stackoverflow.com/questions/62857033/pass-value-from-one-controller-to-another-in-asp-net-core-3-1-mvc/62868845#62868845

    Best Regards,

    Rena

    Tuesday, November 3, 2020 9:42 AM
  • User-1408067692 posted

    redirect to action is expecting an object of route values, or a simple object

        new { param1 = "p1", param2 = "p2"} 

    it does not support a list, nor complex properties and especially the results of a database query.

    as the browser can not call the Index method except via ajax, not sure the point. just change GetTools to 

            public IActionResult GetTools(string divisions)
            {
                var tools = _context.ToolDivision.Where(t => t.division == divisions).ToList();
    
                return Index(tools);
            }

    When I change the code to what you said, it gives me error saying it doesn't find a view named "GetTools". So what I want is basically going to the Index view and passing the tools data. 

    Thanks for replying by the way.

    Tuesday, November 3, 2020 12:54 PM
  • User-1408067692 posted

    Hi KasperMartens,

    You can't pass value from one action to another action by using RedirectToAction.

    You could use VIewBag or TempData...Check the following answer:

    https://stackoverflow.com/questions/62857033/pass-value-from-one-controller-to-another-in-asp-net-core-3-1-mvc/62868845#62868845

    Best Regards,

    Rena

    I'll try to do some research into ViewBag and TempData. Thanks!

    Tuesday, November 3, 2020 12:56 PM
  • User711641945 posted

    Hi KasperMartens,

    Here is the whole working demo:

    List<ToolDivision> data = new List<ToolDivision>() { 
        new ToolDivision(){ division="aaa",tool="a1"},
        new ToolDivision(){ division="bbb",tool="a2"},
        new ToolDivision(){ division="ccc",tool="a3"}
    };
    public IActionResult Index(List<ToolDivision> tools)
        {           
            var divisions = (from td in data select td.division).Distinct().ToList();
            if (TempData["tools"] != null)
            {
                tools = JsonConvert.DeserializeObject<List<ToolDivision>>(TempData["tools"] as string);
            }
            var toolModel = new ToolModel()
            {
                divisions = divisions,
                tools = tools
            };           
            return View(toolModel);
        }
        public IActionResult GetTools(string divisions)
        {
            var tools = data.Where(t => t.division == divisions).ToList();
            TempData["tools"] = JsonConvert.SerializeObject(tools);
            return RedirectToAction("Index", tools);
        }

    Result:

    Best Regards,

    Rena

    Wednesday, November 4, 2020 9:07 AM