简体   繁体   中英

How to change the @Model in a partial view in ASP.NET 5 MVC

I need help to make a partial view. I created a partial view and I want to change model in it. In addition, I would like my partial view to be controlled by a controller.

I don't understand why it's not Ok.

Here is the markup of the view:

<select asp-for="Categorie" id="categorie">
    <script>
        $('#categorie').load('/Home/BudgetSearch');
    </script>
</select>

Here is my controller:

public IActionResult BudgetSearch()
{
        //var bdd = new ComptesBudgetViewModel();

        var query = _context.Budget.Select(oa => oa.Categorie).Distinct();

        return PartialView("BudgetSearch", query.ToList());
}

Here is the code of my partial view:

@model Comptes.core.data.Models.Budget

<option value="" selected>Choisir</option>
@foreach (var budget in Model.Categorie)
{
    <option value="@budget">@budget</option>
}

Can someone help me solve this problem?

Assuming that your badget has a Category property, change your code:

@model IEnumerable<Comptes.core.data.Models.Categorie>

<option value="" selected>Choisir</option>
@foreach (var category in @Model)
{
    <option value="@category.Name">@category.Name</option>
}

and maybe you need to change your action:

public IActionResult BudgetSearch()
{
        //var bdd = new ComptesBudgetViewModel();

        var query = _context.Set<Categorie>().ToList()

        return PartialView("BudgetSearch", query);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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