在强类型视图中使用多个部分视图(ASP.NET MVC4)

12 浏览
0 Comments

在强类型视图中使用多个部分视图(ASP.NET MVC4)

我有两个强类型的部分视图需要在一个强类型的视图中显示。我使用了仓储模式。

下面是父模型的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BOL
{
    public class HomeMultipleBinder
    {
        public IEnumerable g { get; set; }
        public IEnumerable t { get; set; }
    }
}

以下是控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BOL;
namespace TeamBuildingCompetition.Areas.Common.Controllers
{
    public class gHomeController : BaseCommonController
    {
        // GET: Common/gHome
        public ActionResult Index()
        {
            var model = new HomeMultipleBinder();
            model.g = objBs.gameBs.GetALL();
            model.t = objBs.teamBs.GetALL();
            return View(model);
        }
    }
}

以下是视图代码:

@model BOL.HomeMultipleBinder

@{

ViewBag.Title = "Index";

Layout = "~/Views/Shared/_Layout_new.cshtml";

}

Index

@{

@Html.RenderPartial("_gameView",Model.g);

@Html.RenderPartial("_teamView",Model.t);

}

以下是各自的部分视图代码:

@model IEnumerable

@foreach (var item in Model) {

}

@Html.DisplayNameFor(model => model.gameName)

@Html.DisplayNameFor(model => model.description)

@Html.DisplayNameFor(model => model.content)

@Html.DisplayFor(modelItem => item.gameName)

@Html.DisplayFor(modelItem => item.description)

@Html.DisplayFor(modelItem => item.content)

@model IEnumerable

@foreach (var item in Model) {

}

@Html.DisplayNameFor(model => model.teamName)

@Html.DisplayNameFor(model => model.teamPicture)

@Html.DisplayNameFor(model => model.description)

@Html.DisplayNameFor(model => model.content)

@Html.DisplayFor(modelItem => item.teamName)

@Html.DisplayFor(modelItem => item.teamPicture)

@Html.DisplayFor(modelItem => item.description)

@Html.DisplayFor(modelItem => item.content)

我得到了以下编译器错误 "Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments"

0
0 Comments

问题出现的原因是在视图中无法使用`.DisplayNameFor`方法,因为模型类型为`IEnumerable`。解决方法是删除问题中的`Html.RenderPartial("_gameView",Model.g);`前面的符号。

以下是整理的文章:

在ASP.NET MVC4中,有一个问题导致无法在视图中使用`.DisplayNameFor`方法。问题的起因是模型类型为`IEnumerable`,而这个方法在这种情况下无法使用。

解决方法是通过删除问题中的一行代码来解决。具体来说,删除了`Html.RenderPartial("_gameView",Model.g);`前面的符号。

如果你遇到了类似的问题,你可以参考这个解决方法。同时,你也可以点击这里查看具体讨论过程。

0