【问题标题】:Passing a List/model from a View to a controller将列表/模型从视图传递到控制器
【发布时间】:2015-10-06 04:43:28
【问题描述】:

我需要将模型数据从视图传递到控制器。

我的应用程序是这样工作的: 1. 搜索产品并在视图中返回产品列表。 2. 现在,在用户使用列表中的产品后,我想将包含产品列表的模型传递回不同的控制器以进行进一步处理。以及用户点击的产品名称。

我该怎么做?

视图模型

    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);
    }

任何帮助表示赞赏

【问题讨论】:

  • 我看过类似的问题,但解决方案似乎不起作用

标签: .net model-view-controller view controller


【解决方案1】:

通过废弃这个应用解决了问题哈哈:D

【讨论】:

    猜你喜欢
    • 2019-09-03
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2013-07-08
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多