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()
?– Marcelo Shiniti Uchimura
It didn’t work either :(, is arriving null...
– Gabriel Francisco
Took the
<input asp-for="Search" type="text" />
?– Marcelo Shiniti Uchimura
Change the name of the property
Product
in View Model for something other than her type name, for example,MyProduct
. Same thing forCategory
,MyCategory
. Reference: https://stackoverflow.com/a/23642947/10991202– Marcelo Shiniti Uchimura
Yes I removed the <input>, but still without success again, even renaming
– Gabriel Francisco
In the Httppost method declaration, change
public IActionResult Index(ProductViewModel viewModel)
forpublic IActionResult Index([FromBody] ProductViewModel viewModel)
– Marcelo Shiniti Uchimura
Still unsuccessful... I’m cracking my head here.
– Gabriel Francisco
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, frompublic IActionResult Index(ProductViewModel viewModel)
to, perhaps,public IActionResult Index(ProductViewModel vm)
. Reference: https://stackoverflow.com/a/10749502/10991202– Marcelo Shiniti Uchimura
I put <form class="form-group search" Asp-action="Index" method="post"> and solved. Thank you ;)
– Gabriel Francisco
But now I’m willing to put the Asp-validation-for but it’s not validating
– Gabriel Francisco