1

I have 2 strongly typed partial views to be displayed in a strongly typed view. I used the repository pattern.

Here's the code to parent model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BOL
{
    public class HomeMultipleBinder
    {
        public IEnumerable<game> g { get; set; }
        public IEnumerable<team> t { get; set; }
    }
}

Here're s the controller:

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);
        }
    }
}

and here's the view:

@model BOL.HomeMultipleBinder
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout_new.cshtml";
}

<h2>Index</h2>

@{
    @Html.RenderPartial("_gameView",Model.g);
    @Html.RenderPartial("_teamView",Model.t);
}

and below are the respective partial views:

    @model IEnumerable<BOL.game>

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.gameName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.content)
            </th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.gameName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.content)
            </td>
        </tr>
    }

    </table>

and

@model IEnumerable<BOL.team>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.teamName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.teamPicture)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.content)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.teamName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.teamPicture)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.content)
        </td>
    </tr>
}

</table>

I have the following Compiler Error "Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments"

Guzzyman
  • 561
  • 6
  • 16
  • 37
  • 1
    It needs to be capital M - `Html.RenderPartial("...", Model.g)` And your `Index()` method should return `View()`, not `PartialView()` –  Oct 11 '15 at 00:24
  • 1
    Where is the NullReferenceError? On model.g and model.t the intellisense underline of model? Can you try using Model instead of model? Edit: Welp Stephen's online, might as well go to bed! His solution will work – Daniel Hoffmann-Mitscherling Oct 11 '15 at 00:24
  • @StephenMuecke: I still have errors...Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'g' and no extension method 'g' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) – Guzzyman Oct 11 '15 at 00:36
  • There is nothing in the code you have shown which would cause that. Indicate which line of code throws that error. And indicate the names of each of the views you have shown. And you need to include the code, not images of it. –  Oct 11 '15 at 00:41
  • @StephenMuecke: Html.RenderPartial("_gameView",Model.g); with the g underlined. I have a compilation error System.Collections.Generic.IEnumerable' does not contain a definition for 'g – Guzzyman Oct 11 '15 at 00:49
  • 1
    @Guzzyman, Nowhere in the code you have shown are you passing `IEnumerable' ` to a view. All you have shown is the `Index()` method which passes a single instance of `HomeMultipleBinder` so you have not shown us the correct code. –  Oct 11 '15 at 00:51
  • @StephenMuecke: Below is the model for HomeMultipleBinder 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; } } } – Guzzyman Oct 11 '15 at 00:58
  • @StephenMuecke: line var model = new HomeMultipleBinder(); 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(); } } } – Guzzyman Oct 11 '15 at 01:00
  • You need to put your code in the question, not in comments. And you need to delete your images and replace them with the actual code. But my previous comments stand - you have **not** shown us the relevant code - the error message refers to `IEnumerable` but nowhere to you have code that generates a collection of `HomeMultipleBinder` –  Oct 11 '15 at 01:02
  • @Guzzyman, It is impossible to read your code in comments especially when you don't even format it! And now your method has `return View()` when it needs to be `return View(model)` other wise you throw exceptions because `model` is `null` –  Oct 11 '15 at 01:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91942/discussion-between-guzzyman-and-stephen-muecke). – Guzzyman Oct 11 '15 at 01:07

1 Answers1

0

I don't think you are able to write this in your views:

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

your model type is IEnumerable

Is not it giving any errer!!?

this might help

Community
  • 1
  • 1
Mahmoud Hboubati
  • 1,007
  • 11
  • 26
  • This has been resolved by Stephen Muecke in a private chat. I simply removed the sign before the Html.RenderPartial("_gameView",Model.g); in my question above and it worked. – Guzzyman Oct 12 '15 at 00:10