Problem sending View information to Controller

Asked

Viewed 35 times

1

I’m doing a simple CRUD on ASP.Net MVC and implemented a search box to search for a product in the database. But when I click search, the Viewmodel item passed in the Taghelpers is not sent to the controller. What may be happening?

I ran the application in Debug mode and the value of the "Search" property is always null, checking whether the value and null is returning Notfound().

View code:

 @model WebStorageMVC.ViewModels.ProductViewModel

    @using (Html.BeginForm("Index", "Products", FormMethod.Post))
    {
        <div class="form-group search">
            @Html.HiddenFor(m => m.Search)
            <label asp-for="Search">Search</label>
            <input asp-for="Search" type="text" />
            <input type="submit" class="btn btn-primary text-light" asp-action="Index" value="Go!" />
        </div>
    }

Controller Code:

public class ProductsController : Controller
    {
        private readonly IProductRepository _productRepository;
        private readonly ICategoryRepository _categoryRepository;

        public ProductsController(IProductRepository productRepository, ICategoryRepository categoryRepository)
        {
            _productRepository = productRepository;
            _categoryRepository = categoryRepository;
        }
        public IActionResult Index()
        {
            ProductViewModel viewModel = new ProductViewModel();
            viewModel.Products = _productRepository.Products;
            return View(viewModel);
        }

        [HttpPost]
        public IActionResult Index(ProductViewModel viewModel)
        {
            string search = viewModel.Search;

            if (String.IsNullOrEmpty(search))
            {
                return NotFound();
            }

            IEnumerable<Product> products = _productRepository
                .Products
                .Where(product => product.Name.Contains(search))
                .ToList();

            var productViewModel = new ProductViewModel
            {
                Products = products,
                Categories = _categoryRepository.Categories
            };

            return View(productViewModel);
        }

Code of the Viewmodel:

 public class ProductViewModel
{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<Category> Categories { get; set; }
    public Product Product { get; set; }
    public Category Category { get; set; }
    public string Search { get; set; }
}
  • Instead of @Html.HiddenFor(), didn’t have to be @Html.TextBoxFor()?

  • It didn’t work either :(, is arriving null...

  • Took the <input asp-for="Search" type="text" />?

  • Change the name of the property Product in View Model for something other than her type name, for example, MyProduct. Same thing for Category, MyCategory. Reference: https://stackoverflow.com/a/23642947/10991202

  • Yes I removed the <input>, but still without success again, even renaming

  • In the Httppost method declaration, change public IActionResult Index(ProductViewModel viewModel) for public IActionResult Index([FromBody] ProductViewModel viewModel)

  • Still unsuccessful... I’m cracking my head here.

  • I don’t know if you have a guy with a name ViewModel; if so, try changing the name of the parameter in the method declaration, from public IActionResult Index(ProductViewModel viewModel) to, perhaps, public IActionResult Index(ProductViewModel vm). Reference: https://stackoverflow.com/a/10749502/10991202

  • 1

    I put <form class="form-group search" Asp-action="Index" method="post"> and solved. Thank you ;)

  • But now I’m willing to put the Asp-validation-for but it’s not validating

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.