【问题标题】:Rendering a strongly typed partial view within another strongly typed partial view - incorrect model being passed into child view在另一个强类型部分视图中呈现强类型部分视图 - 将不正确的模型传递到子视图
【发布时间】:2013-07-28 01:29:41
【问题描述】:

我有一个在 _Layout.cshtml 中呈现的强类型局部视图,因此它包含在每个页面中。在该局部视图中,我试图呈现另一个局部视图。我创建了一个视图模型 (ShoutboxView) 用作父视图的模型。尝试在其中渲染子部分视图 (_ShoutList) 时,我收到一个错误,即我传入的模型类型不正确:

传入字典的模型项的类型为“MvcForumTest.ViewModels.ShoutboxView”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[MvcForumTest.Models.Shout]”的模型项。

请看下面的代码:

模型(Shout.cs):

namespace MvcForumTest.Models
{
    public class Shout
    {
        public int Id { get; set; }
        public string Author { get; set; }
        public string Message { get; set; }
        public DateTime EntryDate { get; set; }

        public Shout()
        {
            Id = 1;
            Author = "TestUser";
            EntryDate = DateTime.Now;
        }
    }
}

查看模型 (ShoutboxView.cs)

namespace MvcForumTest.ViewModels
{
    public class ShoutboxView
    {
        public Shout newShout { get; set; }
        public IEnumerable<Shout> Shouts { get; set; }
    }
}

控制器(ShoutController.cs):

namespace MvcForumTest.Controllers
{
    public class ShoutController : Controller
    {
        private ForumsContext db = new ForumsContext();

        public ActionResult Index()
        {
            ShoutboxView shoutboxView = new ShoutboxView
            {
                newShout = new Shout(),
                Shouts = (from s in db.Shouts
                          orderby s.EntryDate descending
                          select s).Take(20)
            };
            return PartialView(shoutboxView);
        }

        [HttpPost]
        public ActionResult AddShout(ShoutboxView shoutbox)
        {
            Shout shout = new Shout();
            shout.Message = shoutbox.newShout.Message;

            if (ModelState.IsValid)
            {
                db.Shouts.Add(shout);
                db.SaveChanges();
                return PartialView("Index", shoutbox);
            }

            //return PartialView("_ShoutList", db.Shouts);
            return PartialView("Index", shoutbox);
        }
    }
}

索引局部视图 (Index.cshtml)。这是我得到错误的地方。在 "@Html.Partial("_ShoutList", Model.Shouts)" 行:

@model MvcForumTest.ViewModels.ShoutboxView

@using (Ajax.BeginForm("AddShout", "Shout", new AjaxOptions { UpdateTargetId = "shoutboxWrapper", InsertionMode = InsertionMode.Replace}))
{
    @Html.EditorFor(model => model.newShout.Message)
    <input type="submit" value="Submit" />
}

<div id="shoutboxWrapper">
    @Html.Partial("_ShoutList", Model.Shouts)
</div>

喊局部视图(_Shoutlist.cshtml):

@model IEnumerable<MvcForumTest.Models.Shout>

@foreach (var item in Model)
{
    <div class="shout">
        <table>
            <tr>
                <td class="shoutDelete">
                @Html.ActionLink("Delete", "Delete", new { id=item.Id })
                </td>
                <td class="shoutAuthor">
                    @Html.DisplayFor(model => item.Author)
                </td>
                <td class="shoutDate">
                    @Html.DisplayFor(model => item.EntryDate)
                </td>
            </tr>
            <tr>
                <td class="shoutMessage">
                    @Html.DisplayFor(model => item.Message)
                </td>
            </tr>
        </table>
    </div>
}

我该如何解决这个问题?我应该调用@Html.Partial 以外的其他东西来呈现子部分视图吗?还是我应该通过其他方式传入子模型?

编辑:

_Layout.cshtml:

<div id="body">
     @RenderSection("featured", required: false)
     <section class="content-wrapper main-content clear-fix">
          @Html.Action("Index", "Shout")
          @RenderBody()
     </section>
</div>

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:

    我认为你应该选择 Shouts AsEnumerable()。

                return PartialView("_ShoutList", db.Shouts.AsEnumerable());
    

    【讨论】:

    • 如果我在 AddShout 中使用它而不是返回 PartialView("Index",shoutbox),它会在自己的页面上自行返回“_ShoutList”部分视图。
    【解决方案2】:

    我已经重新创建了您的项目,并在 ShoutController 中进行了以下更改:

    var shouts = (from s in db.Shouts
                      orderby s.EntryDate descending
                      select s)
                      .Take(20)
                      .ToList();
    
    shouts.Add(new Shout{Author = "T", EntryDate = DateTime.Now, Id = 1, Message = "some message"});
    shouts.Add(new Shout { Author = "T2", EntryDate = DateTime.Now, Id = 2, Message = "some message2" });
    
    var shoutboxView = new ShoutboxView
    {
        newShout = new Shout(),
        Shouts = shouts
    };
    
    return PartialView(shoutboxView);
    

    访问 ~/Shout/Index 时加载正常。您是说在 _Layout.cshtml 中,有一个强类型的局部视图。索引是局部视图吗?如果是这种情况,使用 _Layout.cshtml 的完整视图是什么,该视图的操作方法是什么?那将是要查看的代码。

    【讨论】:

    • 我已经编辑了原始帖子以包含 _Layout.cshtml 中的内容。 Index 是在 _Layout 中加载的局部视图。这是我希望出现在使用 _Layout.cshtml 的所有视图上的内容。
    • 我重新创建了布局并添加了一个没有任何内容的完整视图,它对我来说效果很好。在 Index.cshtml 中,如果我将 @Html.Partial("_ShoutList", Model.Shouts) 更改为 @Html.Partial("_ShoutList", Model),那么我会看到与您相同的错误。您确定正在渲染这个“索引”视图,而不是来自不同控制器的另一个视图吗?
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 2012-09-30
    • 2012-06-30
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多