locked
Redirect Problem RRS feed

  • Question

  • User1750806012 posted

    I want to redirect to another page but i get this error:

    InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.AspNetCore.Mvc.RedirectResult', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[Webshop.Models.Categorys]'.

    This is my view:

    @model IEnumerable<Webshop.Models.Categorys>
    
    <form asp-action="Create" method="post">
    
     <select name="Category">
                        <option ></option>
                        @foreach (var item in Model)
                        {
                            <option>@item.category</option>
                        }
                    </select>
    <input type="text" name="price" placeholder="price" />
    <input type="text" name="Description" placeholder="Description" />
    <button type="submit">Create</button> </form>

    I need @model to be a IEnumerable so i can pass the categorys into the select input
    after i filled it in I want it to redirect to the index that is when i get the error 

    my controller:

     public IActionResult Create()
            {
                var CreateSelect = from m in _context.Categorys
                                   select m;
                return View(CreateSelect);
            }
     public async Task<IActionResult> Create(string Category, int price, string Description,
    
    // some code..
    
     return View(RedirectToAction(nameof(Index)));
            }
    

    I think asp net core expects me to return the Category list
    but i dont want that i just want it to redirect to another page 
    because the rest of the code works perfectly fine 

    Wednesday, April 14, 2021 6:48 PM

Answers

  • User475983607 posted

    The syntax is

    return RedirectToAction(nameof(Index));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 14, 2021 7:27 PM