Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding 2 IEnumerable models in 1 view

I created a view that was working successfully with 1 view

@model IEnumerable<string>
<ul>
    @foreach (var fName in Model)
    {
        var name = fName;
        var link = @Url.Content("~/Content/archives/mgamm/") + name.Replace(" ", "%20");

        <li style="list-style:none; font-size:1.2em;">
            <a href="@link">@name</a>
        </li>
    }
</ul>
@if (User.IsInRole("admin"))
{
    <div>
        @using (Html.BeginForm("Index", "Archives", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <input type="File" name="file" id="file" value="Choose File" />
            <button type="submit">Upload</button>
        }
    </div>
}

With a Controller

namespace plantationmvc.Controllers
{
    public class ArchivesController : Controller
    {
        //
        // GET: /Archives/
        public ActionResult Index()
        {
            var path = Server.MapPath("~/Content/archives/mgamm");

            var dir = new DirectoryInfo(path);

            var files = dir.EnumerateFiles().Select(f => f.Name);

            return View(files);
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            var path = Path.Combine(Server.MapPath("~/Content/archives/mgamm"), file.FileName);

            var data = new byte[file.ContentLength];
            file.InputStream.Read(data, 0, file.ContentLength);

            using (var sw = new FileStream(path, FileMode.Create))
            {
                sw.Write(data, 0, data.Length);
            }

            return RedirectToAction("Index");
        }
    }
}

However I wanted to add another snippet like this on the same page, but with a different content path.

How do I add another model to this page?

I just had a controller and View, so I created a ViewModel creating 2 classes

namespace plantationmvc.Models
{
    public class ArchivesViewModel
    {
        public CommModel Model1 { get; set; }
        public MeetModel Model2 { get; set; }
    }

    public class CommModel
    {
        public IEnumerable<CommModel>              
    }
    public class MeetModel 
    {
        public IEnumerable<MeetModel>
    }
}

When I try to pass this into my view as @model IEnumerable<plantationmvc.Models.CommModel> it says it does not exist in the namespace.

like image 850
jtrdev Avatar asked Aug 06 '15 21:08

jtrdev


People also ask

Can we use 2 models in a view?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.

What is IEnumerable in MVC view?

IEnumerable will execute select query on server side, load data in-memory on client side and then filter data. IEnumerable can be used for querying data from in-memory collections like List, Array etc.

How do I access model value in view?

In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie. Models) for the Model class.


2 Answers

{
 public class ArchivesViewModel
 {
    public IEnumerable<CommModel> Model1 { get; set; }
    public IEnumerable<MeetModel> Model2 { get; set; }
 }

 public class CommModel
 {
    //properties of CommModel 

 }
 public class MeetModel 
 {
   //properties of Meet Model
 }

 }

And add the view @model plantationmvc.Models.ArchivesViewModel

like image 89
MstfAsan Avatar answered Sep 28 '22 05:09

MstfAsan


You need any type for your model which has two collections in it.

It can be either your type (e.g. ArchivesViewModel from the answer above) or you can even use Tuple<T1, T2>.

Controler:

public ActionResult Index()
{
    var list1 = new[] { "1", "2", "3", "4", "5" };
    var list2 = new[] { "10", "20", "30", "40", "50" };
    var model = Tuple.Create<IEnumerable<string>, IEnumerable<string>>(list1, list2);
    return View(model);
}

View Index.schtml:

@model Tuple<IEnumerable<string>, IEnumerable<string>>

@foreach (var a in Model.Item1)
{
    <h2>@a</h2> 
}
@foreach (var b in Model.Item2)
{ 
    <h3>@b</h3>
}
like image 35
Ilya Palkin Avatar answered Sep 28 '22 04:09

Ilya Palkin