繁体   English   中英

如何使用 C# 在 ASP.NET MVC 中获取可变的 asp-route-parameter?

[英]How to get a variable asp-route-parameter in ASP.NET MVC with C#?

我想知道是否有人知道我如何从我的表单选择选项中获取选定的值到我的 asp-route-brand 和 asp-route-price 中。 ASP.NET MVC 的新手,它用于分配)。

这是我的观点:

<form name="formid">
    <a>Filter by brand</a>
    <select name="brandoption">
        @foreach (string brand in Model.Brands)
        {
            <option id="brand" value="@brand">@brand</option>               
        }
    </select>
</form>

<form>
    <a>Filter by price</a>
    <select name="priceoption">

        @for (int i = 25; i < 200; i += 25)
        {
            <option value="@i">@i €</option>
        }
    </select>
</form>

<a id="btn" asp-controller="Boards"
   asp-action="FilterBoards" asp-route-brand=""  asp-route-price ="" class="btn btn-primary " >
  FIlter.
</a>

asp-route-brandasp-route-price将在应用程序运行时生成到 url 的查询字符串。 所以你不能根据表单选择来设置这些标签助手的值。

我认为方便的方法是您可以使用表单提交来实现您的要求:

<form name="formid" asp-controller="Boards" 
               asp-action="FilterBoards">   @*add tag helper here*@
    <a>Filter by brand</a>
    <select name="brand">   @*change the name*@
        @foreach (string brand in Model.Brands)
        {
            <option id="brand" value="@brand">@brand</option>
        }
    </select>
    <a>Filter by price</a>
    <select name="price">   @*change the name*@

        @for (int i = 25; i < 200; i += 25)
        {
            <option value="@i">@i €</option>
        }
    </select>
    <input type="submit" value="Filter"/>
</form>

后台代码:

public void FilterBoards(string brand, string price)//the name attribute in frontend 
                                                   //matches the parameter name in backend
{
    //....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM