如何根据用户单击的按钮填充字段?

How do I populate a field depending on which button a user clicks?

这类似于使用 asp.net 核心的“添加到购物车”功能。因此,例如,如果有人点击汉堡图片旁边的按钮,它应该被重定向到 Order 创建页面,ProductName 字段填充文本“Burger”。 我试过使用 @Html.ActionLink 但这只会将您定向到一个新页面并且不会填充该字段。按下按钮时填充字段的替代方法有哪些。

This is the button that needs to populate the ProductName field

This is what the end result should be

任何提示或建议将不胜感激

您可以使用 asp-route taghelper 将数据传递到后端。

 <a asp-action="Create" asp-route-name="@Model.Name">ADD TO CART</a> |

然后在你的 Create action.Use ViewData 中将名称传递给视图。

 public IActionResult Create(string name)
    {
        ViewData["Name"] = name;
        return View();
    }

然后在您看来,将您的代码更改为:

 <input value="@ViewBag.Name" name="ProductName" class="form-control" />

获取您发送的姓名。