I need to pass a model data from a view to a controller.
My app works like this: 1. Search for products and return the list of products in a view. 2. Now after user on a product in a list I want to pass that model which contains the list of the products back into a different controller for further processing. As well as the name of the product that the user has clicked on.
How can I do this?
ViewModel
public class ProductViewModel
{
public ProductViewModel()
{
products = new List<MorrisonsProduct>();
}
public MorrisonsProduct product { get; set; }
public IList<MorrisonsProduct> products { get; set; }
}
View:
@model ProjectSeedplanter.Models.ProductViewModel
<div class="container">
<br />
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div class="input-field ">
@Html.TextBox("query", "")
</div>
<button type="submit" class="btn waves-effect waves-light bottom-sheet">Search</button>
}
<br />
@if (Model != null && Model.products.Count > 0)
{
<table class="table-bordered">
<tr>
<th>Image</th>
<th>Store</th>
<th>Brand</th>
<th>Product Name</th>
<th>Price</th>
<th>Price per Qty</th>
</tr>
@foreach (var product in Model.products)
{
<tr>
<td><img src="@product.img" height="100" width="100" /></td>
<td><span>@product.store</span></td>
<td><span>@product.brand</span></td>
<td>@Html.ActionLink(product.name, "Search", new { prod = product, prodlis = Model.products })</td>
<td><span>£@product.price</span></td>
<td><span>£@product.pricepQty</span></td>
</tr>
}
</table>
}
</div>
Controllers
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string query)
{
List<MorrisonsProduct> product = test.Search(query);
Models.ProductViewModel model = new Models.ProductViewModel();
model.products = product;
return View(model);
}
public ActionResult Search(ProductViewModel model)
{
//work here
return View("Index", product);
}
Any help appreciated