【问题标题】:MVC querying in view or in controller, with different resultsMVC 在视图或控制器中查询,结果不同
【发布时间】:2020-04-04 10:18:00
【问题描述】:

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

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

是否存在性能问题或任何其他问题?我究竟做错了什么?

感谢您的帮助。

代码如下:

(1)案例运行正常

查看:

@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) 案例未运行

查看:

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

【问题讨论】:

  • 您能否为“_IndexClienti”视图添加代码以获得更好的想法
  • 你能查一下@Html.Partial("_IndexClienti",Model)
  • 使用Partial你想调用控制器动作吗?
  • @jishansiddique,查询没有给出正确的结果
  • @Alberto,如果您在主视图中渲染部分视图并且您的视图具有模型类,则需要使用部分帮助器发送。

标签: asp.net-mvc asp.net-mvc-partialview


【解决方案1】:

这里给你一些解释

我有点困惑你的第一个案例是如何运行的,但没关系,因为没有完整的代码很难回答。在使用 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,我建议您使用它们

【讨论】:

    【解决方案2】:

    你可以试试这个

    控制器代码

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

    视图“_IndexClienti”必须与action同名,否则需要返回如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>
    

    【讨论】:

    • 对不起,但它没有运行,我做错了,因为如果我直接在浏览器中启动 partialview _IndexClienti 地址,我显然会在没有 _layout 方面的情况下获得正确的结果。
    【解决方案3】:

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

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

    谢谢大家

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多