Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normal model and IEnumerable model in same view

Have a little problem, which I cannot solve by myself. I want to use same model in one view, but once with IEnumerable<> and second time without it.

My View:

@model IEnumerable<AMBIT_CMS_MVC.Areas.Admin.Models.Product>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>DYSKRET - FOLIE OKIENNE</title>
    @Styles.Render("~/Content/Site.css")
    @Styles.Render("~/Content/bootstrap.css")
    @Scripts.Render("~/Scripts/jquery-1.10.2.min.js")
    @Scripts.Render("~/Scripts/bootstrap.js")
    @Scripts.Render("~/Scripts/buttons.js")
</head>
<body>
    <div class="container-fluid okienne">
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-8 menu">
                <a href="http://dyskret.pl/" target="_blank">
                    <img src="~/Images/logo.png" class="img-responsive fimg" />
                </a>
            </div>
            <div class="col-sm-2"></div>
        </div>
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-2 lista">
                <ul>
                    @foreach (var item in Model)
                    {
                        <li>@Html.ActionLink(item.Name, "Produkt", new { ProductID = item.ProductID })</li>
                    }
                </ul>
            </div>
            <div class="col-sm-6 details">
                @Html.DisplayFor(m => m.Name)
            </div>
            <div class="col-sm-2"></div>
        </div>
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-8 footerProducts">
                Text...
            </div>
            <div class="col-sm-2"></div>
        </div>
    </div>
</body>
</html>

The problem is that foreach loop will work perfectly (I want to list all the products in this category), but when clicked I want to show also details of it on right, but I cannot because the model is IEnumerable<> and should be simple model (AMBIT_CMS_MVC.Areas.Admin.Models.Product). It's like I want to have Index and Details views in one. How can I achieve this?

like image 931
Ashiv3r Avatar asked Jul 03 '26 13:07

Ashiv3r


1 Answers

Using Tuple method normal model and IEnumerble model

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectX.Models;

namespace ProjectX.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            ProjectX.DAL.ProjectContext db = new ProjectX.DAL.ProjectContext();
            // DAL is Data Access Layer Folder's name

            return View(Tuple.Create<Student, IEnumerable<Student>>(new Student(), db.Student.ToList()));
        }
    }
}

View:

@model Tuple<ProjectX.Models.Student,IEnumerable<ProjectX.Models.Student>>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_StudentLayout.cshtml";
}

<table>
    <thead>
        <th>
            @Html.DisplayNameFor(model => model.Item1.student_name)
        <th />
        <th>
            @Html.DisplayNameFor(model => model.Item1.student_surname)
        <th />
    <thead />
    <tbody>
        @foreach (var item in Model.Item2)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.student_name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.student_surname)
                </td>
            </tr>
        }
    <tbody />
 </table>
like image 78
Ahmet YALÇIN Avatar answered Jul 06 '26 02:07

Ahmet YALÇIN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!