在另一个强类型的局部视图中渲染强类型的局部视图 - 将错误的模型传递到子视图中

时间:2013-07-28 01:07:58

标签: asp.net-mvc razor

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

  

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

请参阅以下代码:

模型(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; }
    }
}

Controller(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)”line:

@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>

2 个答案:

答案 0 :(得分:1)

我通过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的完整视图是什么?那个动作方法是什么?这将是要查看的代码。

答案 1 :(得分:0)

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

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