Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在另一个强类型局部视图中呈现强类型局部视图-将不正确的模型传递到子视图_Asp.net Mvc_Razor - Fatal编程技术网

Asp.net mvc 在另一个强类型局部视图中呈现强类型局部视图-将不正确的模型传递到子视图

Asp.net mvc 在另一个强类型局部视图中呈现强类型局部视图-将不正确的模型传递到子视图,asp.net-mvc,razor,Asp.net Mvc,Razor,我有一个强类型的局部视图,它在_Layout.cshtml中呈现,因此它包含在每个页面中。在这个局部视图中,我试图渲染另一个局部视图。我已经创建了一个视图模型(ShoutboxView),用作父视图的模型。当试图在其中渲染子部分视图(_ShoutList)时,我得到一个错误,即我传递的模型的类型不正确: 传递到字典的模型项的类型为“MvcForumTest.ViewModels.ShoutboxView”,但此字典需要类型为“System.Collections.Generic.IEnumera

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

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

请参阅下面的代码:

型号(Shout.cs):

视图模型(ShoutboxView.cs)

索引部分视图(Index.cshtml)。这就是我得到错误的地方。在“@Html.Partial(“\u ShoutList”,Model.Shouts)”行上:


我认为您应该选择Shouts AsEnumerable()


我已使用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的完整视图是什么?该视图的操作方法是什么?这就是要查看的代码。

如果我在AddShout中使用它,而不是返回PartialView(“Index”,shoutbox),它会在自己的页面上自行返回“\u ShoutList”部分视图。我已经编辑了原始帖子,将其包含在\u Layout.cshtml中。索引是正在_布局中加载的局部视图。这是我希望在所有使用_Layout.cshtml的视图上显示的内容。我重新创建了布局,并添加了一个没有任何内容的完整视图,而且对我来说效果很好。在Index.cshtml中,如果我将@Html.Partial(“\u ShoutList”,Model.Shouts)更改为@Html.Partial(“\u ShoutList,Model”),那么我将看到与您相同的错误。您确定正在呈现此“索引”视图,而不是来自不同控制器的另一个视图吗?
namespace MvcForumTest.ViewModels
{
    public class ShoutboxView
    {
        public Shout newShout { get; set; }
        public IEnumerable<Shout> Shouts { get; set; }
    }
}
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);
        }
    }
}
@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>
@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>
}
<div id="body">
     @RenderSection("featured", required: false)
     <section class="content-wrapper main-content clear-fix">
          @Html.Action("Index", "Shout")
          @RenderBody()
     </section>
</div>
            return PartialView("_ShoutList", db.Shouts.AsEnumerable());
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);