ASP.NET MVC Core将视图中的dropdownlist中的值传递给控制器

时间:2017-03-18 15:31:19

标签: 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 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; }
}

存储我正在使用EF Core的数据

public class StoreContext : DbContext
{
    public StoreContext(DbContextOptions<StoreContext> options) : base(options) { }

    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

在我的ProductsController中,我有以下GET方法:

public async Task<IActionResult> Index(int category, string search)
{
    var storeContext = _context.Products.Include(p => p.Category) as IQueryable<Product>;


    if (category != 0)
    {
        storeContext = storeContext.Where(p => p.Category.ID == category);
    }

    if (search != null)
    {
        storeContext = storeContext.Where(p => p.Category.Name == search);
    }

    var categories = storeContext.OrderBy(p => p.Category.Name).Select(p => p.Category.Name).Distinct();

    if (!String.IsNullOrEmpty(search))
    {
        storeContext = storeContext.Where(p => p.Name.Contains(search)||
        p.Description.Contains(search) ||
        p.Category.Name.Contains(search));
        ViewBag.Search = search;
    }

    ViewBag.Category = new SelectList(categories);

    return View(await storeContext.ToListAsync());
}

我的Index.cshtml如下所示:

@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>

按“过滤器”按钮应该会刷新视图并按Category.Name

过滤

但是,遗憾的是它不起作用。 在我的浏览器中,我读了

http://localhost:53827/Products?search=

因此没有参数传递给搜索。

如果我手工输入

http://localhost:53827/Products?search=clothes 

它完美无缺

在Startup.cs中,我有以下路线

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

知道怎么解决吗?

0 个答案:

没有答案