【问题标题】:C# MVC4 Partial View with other ActionResult in ControllerC# MVC4 部分视图与控制器中的其他 ActionResult
【发布时间】:2014-09-02 00:54:46
【问题描述】:

我有问题。 我有我的控制器“DashboardNB2Controller”,我的视图“index.cshtml”,我想在我的“index.cshtml”中集成一个名为“_PartialView.cshtml”的局部视图。两个视图都在同一个文件夹中。在我的控制器中,我的局部视图中有数据库操作的“ActionResult _PartialView”。

但如果我将部分视图集成到索引视图中,则操作结果“_PartialView”不起作用。我没有得到任何结果。我的数据库的查询是正确的。我检查了这个。

这是我的代码

  1. 我的控制器带有部分视图的 ActionResult

    public ActionResult _PartialView()
    {
        var lastMessages= (from t in db.view_tbl_message
                                         orderby t.Date descending
                                         select t).Take(10);
    
    
    
        ViewModelDashboard model = new ViewModelDashboard();
        model.view_tbl_message = lastMessages.ToList();
    
    
        return PartialView("_PartialView", model);
    
    }
    

我的 index.cshtml

 @model AisWebController.Areas.Statistics.Models.ViewModelDashboard


@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";

 }

<br />
     @{Html.Action("_PartialView", "DashboardNB2");}

<br />

还有我的 _PartialView.cshtml

 @model WebApplication.Areas.Stats.Models.ViewModelDashboard

 <table class="table table-bordered">
<tr>
        <th>
            Date
        </th>
        <th>
            User
        </th>
        <th>
            Message
        </th>

    </tr>
 @foreach (var item in Model.view_tbl_message)
 {

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Date
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.User)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Message) 
            </td>

        </tr>


    }
</table>

如果有人可以提供帮助,那就太棒了!

【问题讨论】:

    标签: asp.net-mvc c#-4.0 razor model-view-controller partial-views


    【解决方案1】:

    改变

     @{Html.Action("_PartialView", "DashboardNB2");}
    

     @Html.Action("_PartialView", "DashboardNB2")
    

    【讨论】:

    • yw :) 。将 {} 用于 RenderAction(),因为它直接写入响应流
    【解决方案2】:

    在为 Html 扩展方法查看 @ 之后,您不需要 {} 括号

    看看你的 @Html.DisplayFor 它没有任何 {} 括号。

    同样适用于@Html.ActionLink

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2017-12-17
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多