C# ASP.NET MVC核心将视图中dropdownlist的值传递给控制器
我正在(也许)一个简单的挑战中挣扎。 我有以下课程:C# ASP.NET MVC核心将视图中dropdownlist的值传递给控制器,c#,asp.net-core-mvc,entity-framework-core,C#,Asp.net Core Mvc,Entity Framework Core,我正在(也许)一个简单的挑战中挣扎。 我有以下课程: public class Product { public int ID { get; set; } [Display(Name ="Product Name")] public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public
public class Product
{
public int ID { get; set; }
[Display(Name ="Product Name")]
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int? CategoryID { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public int ID { get; set; }
[Display(Name = "Category Name")]
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
因此,没有向搜索传递任何参数
如果我亲自进去
http://localhost:53827/Products?search=clothes
它工作得很好
在Startup.cs中,我有以下路线
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
知道如何解决它吗?在浏览器调试器中,转到“网络”选项卡,在按下“过滤器”按钮时查看它正在调用的URL。这将引导你走向正确的方向。嗨,CodingYoshi,这确实是个问题。浏览器被重定向到,而不向搜索字符串传递任何参数。我在select元素上没有看到要将值传递回控制器的名称属性。使用
name=“search”
的唯一输入是隐藏输入(它是将被发回的隐藏输入的值)。您的选择没有名称
属性,其值将不会被发布回亲爱的Ashley,亲爱的Stephen非常感谢您的帮助。这确实是问题所在。现在它工作得很好!
@model IEnumerable<BabyStore.Models.Product>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-action="Index" asp-controller="Products" method="get">
<div class="form-group">
<table>
<tr>
<td>
<label>Filtered by Category: </label>
</td>
<td>
<select class="form-control" asp-items="ViewBag.Category">
<option value="">-- Select Category --</option>
</select>
</td>
<td>
<input type="submit" value="Filter" id="search"/>
<input type="hidden" name="search" id="search"/>
</td>
</tr>
</table>
</div>
</form>
<br />
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Category.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
http://localhost:53827/Products?search=
http://localhost:53827/Products?search=clothes
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});