MVC 在视图或控制器中查询,结果不同

MVC querying in view or in controller, with different results

我正在尝试使用 Visual Studio 2019 创建一个 ASP.NET MVC 应用程序,但我在部分视图查询方面遇到了一些问题。

如果我在视图中编写查询并调用 @Html.partial,它们 运行,否则如果我在控制器中编写查询并创建部分视图,当我调用 @Html.partial 他们 运行 不正确。

是否有性能问题或其他问题?我究竟做错了什么?

感谢您的帮助。

代码如下:

(1) case 运行宁正确

查看:

@model IEnumerable<archea.Models.T_Anagrafica>

@Html.Partial("_Index", Model.Where(p => p.Cliente == true))

控制器:

// GET: T_Anagrafica
public ActionResult _Index()
{
    return PartialView(db.T_Anagrafica.OrderBy(den => den.Denominazione).ToList());
}

(2) 大小写不 运行ning

查看:

@model IEnumerable<archea.Models.T_Anagrafica>
@Html.Partial("_IndexClienti")

控制器:

// GET: T_Anagrafica
public ActionResult _IndexClienti()
{
    return View(db.T_Anagrafica.Where(p => p.Cliente == true).OrderBy(den => den.Denominazione).ToList());
}

你能试试这个吗

Controller code

public ActionResult _IndexClienti()
{
     var data=db.T_Anagrafica.Where(p => p.Cliente == true).OrderBy(den => den.Denominazione).ToList();
    return PartialView(data);
}

View "_IndexClienti" must be the same name with action or you need to return like eg return PartialView("_IndexClienti",data);

@model IEnumerable<archea.Models.T_Anagrafica>
<h3>Clienti</h3>

<table class="table">
    <tr>
        <th class="small">
            @Html.DisplayNameFor(model => model.IdAnagrafica)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Denominazione)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Indirizzo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CF)/<BR>
            @Html.DisplayNameFor(model => model.PIVA)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Tel)<BR>
            @Html.DisplayNameFor(model => model.FAX)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CodAnacf)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Fornitore)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Cliente)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Ente)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td class="small">
                @Html.DisplayFor(modelItem => item.IdAnagrafica)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Denominazione)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Indirizzo)<BR>
                @Html.DisplayFor(modelItem => item.CAP) -
                @Html.DisplayFor(modelItem => item.Citta)
                (@Html.DisplayFor(modelItem => item.Provincia))
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CF)<BR>
                @Html.DisplayFor(modelItem => item.PIVA)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Tel)<br>
                @Html.DisplayFor(modelItem => item.FAX)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CodAnacf)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Fornitore)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cliente)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Ente)
            </td>
            <td>

                <div class="btn-group">
                    <a href='@Url.Action("Edit", new { Controller = "T_Anagrafica", action = "Edit", id = item.IdAnagrafica })' class="btn btn-success">
                        <span class="glyphicon glyphicon-edit"></span>
                    </a>
                    <a href='@Url.Action("Details", new { Controller = "T_Anagrafica", action = "Details", id = item.IdAnagrafica })' class="btn btn-primary">
                        <span class="glyphicon glyphicon-list"></span>
                    </a>
                    <a href='@Url.Action("Delete", new { Controller = "T_Anagrafica", action = "Delete", id = item.IdAnagrafica })' class="btn btn-danger">
                        <span class="glyphicon glyphicon-trash"></span>
                    </a>
                </div>
            </td>
        </tr>
    }

</table>

这里有一些解释给你

我有点困惑你的第一个案例如何 运行 但没关系,因为没有完整的代码很难回答。使用 MVC 时,您必须了解这 4 个方法

Html.Partial
Html.RenderPartial
Html.Action
Html.RenderAction

您可以在 2 个不同的地方对结果进行填充或应用排序,但只能在一个地方完成,因此对于第一种情况,它必须看起来像这样

db.T_Anagrafica.Where(p => p.Cliente == true).OrderBy(den => den.Denominazione).ToList()

在你的第二种情况下,你使用的是 Html.Partial,如果你理解这个方法,那么你必须明白你必须传递 @Model 作为参数,如果你不传递那么它不会在你创建时显示任何东西基于模型项目的组件

另一种方法是使用 RenderAction,我建议您使用它们

感谢 Jigar Sangoi,我使用 RenderAction 解决了我的问题,如下所示:

@{Html.RenderAction("_IndexClienti", "T_Anagrafica");}

感谢大家