如何通过asp -...标签发送我的selectlist值? [英] How can I send my selectlist value via asp-... tags?

查看:214
本文介绍了如何通过asp -...标签发送我的selectlist值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

@foreach (var item in vm)
    {

        <tr>
            <td>
                <img src="@Html.DisplayFor(modelItem => item.Image)" style="width:150px" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BrandName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>                  
                <select asp-for="Size">
                @foreach (var i in ViewBag.columnNames){
                    <option value="@i.ToString()">@i</option>
                }
                    </select>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProductType.Description)
            </td>
            <td>
                <a asp-action="AddToCart" asp-route-id="@item.Id">Buy</a>
            </td>
        </tr>
    }

这只是肯定的一部分,但是我有一个问题,关于将选择的值从选择"发送到HomeController的AddToCart方法.我该如何使用asp-route-size或asp-for标签呢?还是有其他方法可以做到?

This is only part for sure, but I have an question about sending my selected value from "select" to AddToCart method of HomeController. How can I do it with asp-route-size or asp-for tags? Or is there any other way to do it?

推荐答案

您可以在控制器中构建选择项,以下是我用来创建仓库选择下拉菜单的一些代码:

You can build your select items in your controller, here's a bit of code I use to create a warehouse selection pulldown:

public List<SelectListItem> WarehouseFilterSelectItems { get; set; }

[BindProperty(SupportsGet = true)]
public string WarehouseFilter { get; set; }

WarehouseFilterSelectItems = _context.Warehouse
                                     .Select(a => new SelectListItem
                                     {
                                         Value = a.ID.ToString(),
                                         Text = a.WarehouseName
                                     }).ToList();

然后在.cshtml中:

Then in the .cshtml:

Warehouse Selection: <select asp-for="WarehouseFilter"
                             asp-items="@Model.WarehouseFilterSelectItems"
                             onchange="ProcessForm('filter_results', 'Inventory/jqindex/'); this.blur();">
                     <option value="0">ALL</option>
                     </select>

如果您不使用javascript发布/返回数据,则不需要"onchange"事件处理程序.

You won't need the 'onchange' event handler if you don't use javascript to post/return the data.

这篇关于如何通过asp -...标签发送我的selectlist值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆