简体   繁体   English

如何在 ASP.NET 5 MVC 的局部视图中更改 @Model

[英]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.我创建了一个局部视图,我想在其中更改 model。 In addition, I would like my partial view to be controlled by a controller.此外,我希望我的部分视图由 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:这是我的 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:假设您的徽章具有 Category 属性,请更改您的代码:

@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);
}

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

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