0

I am trying to use Index view is strongly typed to a collection of @model IEnumerable and also using Model.Name (model=>model.OrganisationName in Html helper methods). Both in the same view as per my requirement.

@*@model IEnumerable<LMTLibrary.LMTUsage>*@
@model LMTLibrary.LMTUsage

<td width="25%">
                            @Html.LabelFor(m => m.Organisation, new { @class = "control-label col-md-2" })
                        </td>
                        <td width="25%">
                            @if (ViewBag.UserRoleId == 1)
                            {
                                @Html.DropDownListFor(m => m.Organisation, new SelectList(Model.LMTOrganisationList, "Organisation", "Organisation"), "All",
                                                                 new { @class = "form-control", @onchange = "javascript:GetBusinessArea(this.value);" })

                            }
                            else
                            {
                                @Html.DropDownListFor(m => m.Organisation, new SelectList(Model.LMTOrganisationList, "Organisation", "Organisation"),
                                                                 new { @class = "form-control", @onchange = "javascript:GetBusinessArea(this.value);" })

                            }
                            @Html.ValidationMessageFor(m => m.Organisation, "", new { @class = "text-danger" })
                        </td>

  @foreach (var item in (IList<LMTLibrary.LMTUsage>)ViewData.Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.UserId)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.UserName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Organisation)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.BusinessArea)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Company)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.RoleName)
                            </td>
                            <td>
                                @Html.ActionLink("Edit", "Edit")
                            </td>
                        </tr>
                    }

Below is my controller code

 public ActionResult Index()
    {
        var LMTAccessRoles = AppSettings.Get<string>("Authorization.LMTAccess");
        var LMTAdminRoles = AppSettings.Get<string>("Authorization.LMTAdmin");

        ViewBag.LMTAccessRoles = LMTAccessRoles;
        ViewBag.LMTAdminRoles = LMTAdminRoles;

        if (!AuthorizationLevel.GetSecurityLevels(User).Contains("LMTAdmin"))
            return RedirectToAction("Unauthorized");

        LMTUsage objLMT = new LMTUsage();
        LMTDAL objLMTDAL = new LMTDAL();

        List<LMTUsage> _OLLMTUsage = new List<LMTUsage>();



        _OLLMTUsage = objLMTDAL.UserRoleCompany_GetAll();

        return View(_OLLMTUsage.ToList());
    }

Which is giving me error. Kindly help me to resolve the issue.

pankaj bawdane
  • 101
  • 4
  • 18

1 Answers1

0

Your view is actually binded with @model LMTLibrary.LMTUsage and not with @*@model IEnumerable<LMTLibrary.LMTUsage>*@ as it is a comment. Infact you cannot bind a controller to multiple models.

Then from the controller, the method is returning List<LMTUsage> which is not expected. As per your needs, either change the controller to return a single object or change the bindings in the view.

If your requirement is such that you need to bind the view with more than one model, then as a good practice use what we call ViewModels, these are nothing but wrappers over multiple models.

Yogi
  • 9,174
  • 2
  • 46
  • 61