将列表/模型从视图传递到控制器 [英] Passing a List/model from a View to a controller
本文介绍了将列表/模型从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将模型数据从视图传递到控制器.
I need to pass a model data from a view to a controller.
我的应用程序的工作方式如下: 1.搜索产品并在视图中返回产品列表. 2.现在,在用户使用列表中的产品之后,我想将包含产品列表的模型传递回另一个控制器,以进行进一步处理.以及用户单击的产品名称.
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.
我该怎么做?
ViewModel
ViewModel
public class ProductViewModel
{
public ProductViewModel()
{
products = new List<MorrisonsProduct>();
}
public MorrisonsProduct product { get; set; }
public IList<MorrisonsProduct> products { get; set; }
}
查看:
@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>
控制器
[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);
}
任何帮助表示赞赏
推荐答案
通过废弃此应用程序可解决问题大声笑:D
Problem solved by scrapping this app lol :D
这篇关于将列表/模型从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文