简体   繁体   English

如何从表中填充 ASP.NET Core MVC 中的下拉列表?

[英]How to populate a dropdownlist in ASP.NET Core MVC from table?

I recently needed to start using Visual Studio again, but it has been over a decade since I've been in there and needless to say quite a bit has changed on me, I realize there are many similar questions.我最近需要再次开始使用 Visual Studio,但是自从我在那里已经有十多年了,不用说我已经发生了很大的变化,我意识到有很多类似的问题。 but I haven't been able to get any of them to work.但我无法让他们中的任何一个工作。 Any assistance would be greatly appreciated!任何帮助将不胜感激!

Here's the situation: I have aa shared search bar layout which currently runs a text search on title.情况是这样的:我有一个共享的搜索栏布局,目前在标题上运行文本搜索。 It needs a dropdown for category which should compile from the Category1 table.它需要一个应从 Category1 表编译的类别下拉列表。

View (Trees/Index):查看(树/索引):

<form asp-controller="Trees" asp-action="Index" method="get">
    <p>
        Category: @*select tag here*@
        Title: <input type="text" name="SearchString" />
        <input type="submit" value="Filter" />
    </p>
</form>

Model (Category1.cs): Model(Category1.cs):

public class Category1
{
    public int Id { get; set; }
    public string CategoryName { get; set; }
    public byte Active { get; set; }
}

Controller (TreesController.cs): Controller(TreesController.cs):

public async Task<IActionResult> Index(string searchString)
{
    var trees = from f in _context.Tree
                select f;

    if (!string.IsNullOrEmpty(searchString))
    {
        trees = trees.Where(s => s.Title.Contains(searchString));
    }

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

I've removed everything I tried and reverted back to the base search functionality.我已经删除了我尝试过的所有内容,并恢复了基本搜索功能。 Please let me know if you need more information of if I have been unclear.如果您需要更多关于我不清楚的信息,请告诉我。 Thank you in advance!先感谢您!

You can change your code like below:您可以更改您的代码,如下所示:

public async Task<IActionResult> Index(string searchString,int category1id)
{
    var trees = from f in _context.Tree
                select f;

    if (!string.IsNullOrEmpty(searchString))
    {
        trees = trees.Where(s => s.Title.Contains(searchString));
    }
    ViewData["Category1"] = new SelectList(_context.Category1.ToList(), "Id", "CategoryName");

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

View:看法:

<form asp-controller="Trees" asp-action="Index" method="get">
<p>
    Category: <select name="category1id" asp-items="ViewBag.Category1">
                <option>Category1</option>
            </select>
    Title: <input type="text" name="SearchString" />
    <input type="submit" value="Filter" />
</p>

Update:更新:

You can create a function like this:您可以像这样创建 function:

public SelectList getCat1(ApplicationDbContext _context)
    {
        return (SelectList)(ViewData["Category1"] = new SelectList(_context.Category1.ToList(), "Id", "CategoryName"));
    }
public async Task<IActionResult> Index()
{
    getCat1(_context);
    return View();
}

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

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